如何安全地检查 JSON 对象是否存在 属性
How do I safely check JSON object for presence of property
我需要在 Logic App 中过滤传入的正文数据,并根据 data.context.activityLog.eventSource
的存在做出不同的反应。我放置了条件语句,但使用 string
函数将 JSON 文档解析为字符串,以查找正文中是否存在 "eventSource": "ResourceHealth"
字符串。我想使用 JSON 解析,但我该如何有效地进行解析,例如 if (data.context.activityLog.eventSource = "ResourceHealth)
。我如何通过逻辑应用程序表达式来做到这一点。传入消息的架构是完全不可预测的,因此可能根本没有 data.context
对象
{
"schemaId": "Microsoft.Insights/activityLogs",
"data": {
"status": "Activated",
"context": {
"activityLog": {
"channels": "Admin, Operation",
"correlationId": "2b8e976a-f15c-4d92-81bb-25d28d542f86",
"eventSource": "ResourceHealth",
"eventTimestamp": "2019-11-28T19:27:39.357+00:00",
"eventDataId": "f9dc6859-2899-4eb2-a6b2-7150acfbc478",
"level": "Critical",
"operationName": "Microsoft.Resourcehealth/healthevent/Activated/action",
"operationId": "f9dc6859-2899-4eb2-a6b2-7150acfbc478",
"properties": {
"title": "We're sorry, your virtual machine isn't available because an unexpected failure on the host server. An unexpected problem with the host is preventing us from automatically recovering your virtual machine.",
"details": null,
"currentHealthStatus": "Unavailable",
"previousHealthStatus": "Available",
"type": "Downtime",
"cause": "PlatformInitiated"
},
"resourceId": "/subscriptions/123/resourceGroups/test/providers/Microsoft.Compute/virtualMachines/NTTTestVM",
"resourceGroupName": "test",
"resourceProviderName": "Microsoft.Resourcehealth/healthevent/action",
"status": "Active",
"subscriptionId": "123",
"submissionTimestamp": "2019-11-28T19:30:40.1798446+00:00",
"resourceType": "MICROSOFT.COMPUTE/VIRTUALMACHINES"
}
}
}
}
由于您的 json 数据的结构是不可预测的,所以我们不能直接使用 "Parse JSON" 操作。请参考下面我的逻辑应用程序:
这里我假设你的数据是一个字符串,我初始化一个名为"input"的变量来存储数据。
然后我们需要从上面的数据中找到字段"eventSource",并将其转换为json格式如{"eventSource":"ResourceHealth" }.
整个表达式是
json(xpath(xml(json(concat('{"root":', variables('input'), '}'))), './/eventSource')[0])
现在变量 "eventSourceJson" 应该是 {"eventSource": "ResourceHealth"}
之后,我们可以很容易地解析出json,如下图所示:
最后我们可以从"Parse JSON"在"If"条件下使用,判断eventSource = "ResourceHealth"
希望对您的问题有所帮助~
我需要在 Logic App 中过滤传入的正文数据,并根据 data.context.activityLog.eventSource
的存在做出不同的反应。我放置了条件语句,但使用 string
函数将 JSON 文档解析为字符串,以查找正文中是否存在 "eventSource": "ResourceHealth"
字符串。我想使用 JSON 解析,但我该如何有效地进行解析,例如 if (data.context.activityLog.eventSource = "ResourceHealth)
。我如何通过逻辑应用程序表达式来做到这一点。传入消息的架构是完全不可预测的,因此可能根本没有 data.context
对象
{
"schemaId": "Microsoft.Insights/activityLogs",
"data": {
"status": "Activated",
"context": {
"activityLog": {
"channels": "Admin, Operation",
"correlationId": "2b8e976a-f15c-4d92-81bb-25d28d542f86",
"eventSource": "ResourceHealth",
"eventTimestamp": "2019-11-28T19:27:39.357+00:00",
"eventDataId": "f9dc6859-2899-4eb2-a6b2-7150acfbc478",
"level": "Critical",
"operationName": "Microsoft.Resourcehealth/healthevent/Activated/action",
"operationId": "f9dc6859-2899-4eb2-a6b2-7150acfbc478",
"properties": {
"title": "We're sorry, your virtual machine isn't available because an unexpected failure on the host server. An unexpected problem with the host is preventing us from automatically recovering your virtual machine.",
"details": null,
"currentHealthStatus": "Unavailable",
"previousHealthStatus": "Available",
"type": "Downtime",
"cause": "PlatformInitiated"
},
"resourceId": "/subscriptions/123/resourceGroups/test/providers/Microsoft.Compute/virtualMachines/NTTTestVM",
"resourceGroupName": "test",
"resourceProviderName": "Microsoft.Resourcehealth/healthevent/action",
"status": "Active",
"subscriptionId": "123",
"submissionTimestamp": "2019-11-28T19:30:40.1798446+00:00",
"resourceType": "MICROSOFT.COMPUTE/VIRTUALMACHINES"
}
}
}
}
由于您的 json 数据的结构是不可预测的,所以我们不能直接使用 "Parse JSON" 操作。请参考下面我的逻辑应用程序:
这里我假设你的数据是一个字符串,我初始化一个名为"input"的变量来存储数据。
然后我们需要从上面的数据中找到字段"eventSource",并将其转换为json格式如{"eventSource":"ResourceHealth" }.
整个表达式是
json(xpath(xml(json(concat('{"root":', variables('input'), '}'))), './/eventSource')[0])
现在变量 "eventSourceJson" 应该是 {"eventSource": "ResourceHealth"}
之后,我们可以很容易地解析出json,如下图所示:
最后我们可以从"Parse JSON"在"If"条件下使用,判断eventSource = "ResourceHealth"
希望对您的问题有所帮助~