如何在 Azure 数据资源管理器中分析复杂的 json 对象



>我有以下属性,我需要解析为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"

需要解析的原始数据

{ "请求正文": "{\"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\",\"Scope\":\"/subscriptions/6f5c5be9-a2dd-49c9-bfa1-77d4db790171\"}}" }

看看这个页面的底部(也在下面引用),它解释了为什么以下工作(顺便说一句,请注意,从效率的角度来看,我已经用has替换了contains):

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

使用描述属性包的 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

最新更新