Gre*_*ian 1 azure-data-explorer
我有以下属性,需要将其解析为 JSON。我尝试使用parse_json()但不起作用
询问
AzureActivity
| where OperationNameValue == "Microsoft.Authorization/roleAssignments/write"
| where ActivityStatus == "Started"
| where (Properties contains "8e3af657-a8ff-443c-a75c-2fe8c4bcb635") or (Properties contains "b24988ac-6180-42a0-ab88-20f7382dd24c")
| extend request = parse_json(Properties)
| where request.requestbody.Properties.Scope == "/subscriptions/6f5c5be9-a2dd-49c9-bfa1-77d4db790171"
Run Code Online (Sandbox Code Playgroud)
需要解析的原始数据
{ "requestbody": "{\"Id\":\"992a2739-9bd2-4d04-bc5f-5ed1142b9861\",\"属性\":{\"PrincipalId\":\"5ac319a4-740b-4f09-9fd3- fce3ce91fedf\",\"RoleDefinitionId\":\"/subscriptions/6f5c5be9-a2dd-49c9-bfa1-77d4db790171/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635\",\"范围\" :\"/subscriptions/6f5c5be9-a2dd-49c9-bfa1-77d4db790171\"}}" }
看一下本页的底部(也在下面引用),这解释了为什么以下内容有效(顺便说一句,请注意,从效率的角度来看,我已经为您替换了): containshas
AzureActivity
| where OperationNameValue == "Microsoft.Authorization/roleAssignments/write"
| where ActivityStatus == "Started"
| where (Properties has "8e3af657-a8ff-443c-a75c-2fe8c4bcb635") or (Properties has "b24988ac-6180-42a0-ab88-20f7382dd24c")
| extend request = parse_json(tostring(parse_json(Properties).requestbody))
| project request.Properties.Scope
Run Code Online (Sandbox Code Playgroud)
使用 JSON 字符串描述属性包(其中一个“槽”是另一个 JSON 字符串)的情况有些常见。
例如:
let d='{"a":123, "b":"{\\"c\\":456}"}'; print d在这种情况下,不仅需要调用 parse_json 两次,而且还要确保在第二次调用中使用 tostring。否则,对 parse_json 的第二次调用将简单地将输入按原样传递给输出,因为其声明的类型是动态的:
let d='{"a":123, "b":"{\\"c\\":456}"}'; print d_b_c=parse_json(tostring(parse_json(d).b)).c