如何实现基于JSON的条件

huangapple go评论57阅读模式
英文:

How to implement JSON based conditions

问题

我正在寻找一种基于JSON应用条件的技巧。请查看下面的示例,目标是筛选记录并仅选择那些具有"isDefault=true"的记录。是否有一种方法可以在不使用字符串搜索的情况下完成,因为字符串搜索似乎不太精确。

let T = datatable (id: int) [1, 2, 3];
T 
| extend json = dynamic([{ "capabilities":"None", "isDefault":false, "isInitial":false, "name":"contoso.fr", "type":"Managed"},
                         { "capabilities":"Mail", "isDefault":true, "isInitial":false, "name":"login.contoso.com", "type":"Managed"},
                         { "capabilities":"Mail,Office", "isDefault":false, "isInitial":false, "name":"contoso.com", "type":"Managed"}])
| extend data = parse_json(json)
| where data.isDefault == true
| project id

希望这是您要的条件。

英文:

I'm looking for a technique to apply a condition based on JSON. Look at the sample below where the goal is to filter the records and only pick those with "isDefault=true". Is there a way to do it without using a string search, which appears sloppy.

let T = datatable (id: int) [1, 2, 3];
T 
| extend json = dynamic([{  "capabilities":"None",  "isDefault":false, "isInitial":false,   "name":"contoso.fr", "type":"Managed"},{  "capabilities":"Mail",  "isDefault":true, "isInitial":false,   "name":"login.contoso.com", "type":"Managed"},{  "capabilities":"Mail,Office",  "isDefault":false, "isInitial":false,   "name":"contoso.com", "type":"Managed"}])
| extend data = parse_json(json)
|  project id // isDefault == "true" //want this to be the condition

答案1

得分: 0

你可以使用mv-apply运算符

例如:

print payload = dynamic(
[
    {"capabilities": "None",        "isDefault": false, "isInitial": false, "name": "contoso.fr",        "type": "Managed"},
    {"capabilities": "Mail",        "isDefault": true,  "isInitial": false, "name": "login.contoso.com", "type": "Managed"},
    {"capabilities": "Mail,Office", "isDefault": false, "isInitial": false, "name": "contoso.com",       "type": "Managed"}
])
| mv-apply with_itemindex = i payload on (
    where payload.isDefault == true
)
payload i
{"capabilities": "Mail","isDefault": true,"isInitial": false,"name": "login.contoso.com","type": "Managed"} 1
英文:

you could use the mv-apply operator

for example:

print payload = dynamic(
[
    {"capabilities": "None",        "isDefault": false, "isInitial": false, "name": "contoso.fr",        "type": "Managed"},
    {"capabilities": "Mail",        "isDefault": true,  "isInitial": false, "name": "login.contoso.com", "type": "Managed"},
    {"capabilities": "Mail,Office", "isDefault": false, "isInitial": false, "name": "contoso.com",       "type": "Managed"}
])
| mv-apply with_itemindex = i payload on (
    where payload.isDefault == true
)
payload i
{<br> "capabilities": "Mail",<br> "isDefault": true,<br> "isInitial": false,<br> "name": "login.contoso.com",<br> "type": "Managed"<br>} 1

huangapple
  • 本文由 发表于 2023年6月1日 07:45:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定