如何在逻辑应用程序中通过表达式语法访问数组值

How do I access an array value through expression syntax in a logic app

我有一个在 Azure 中使用的逻辑应用程序,我需要在其中从 Http 触发器上的某些 JSON 访问特定的嵌套值。

我需要访问阈值 (5) 和维度数组中第二项的值 (accountscontacts-account-deleted) 并通过逻辑应用程序编辑器中的表达式语法动态显示这些但不能弄清楚我的表达哪里出了问题。

我正在尝试使用此语法访问阈值,

first('allOf')?['threshold']

并且我正在尝试使用此表达式来访问第二维值,

last(first('allOf')?['dimensions'])?['value']

这些似乎都不起作用,我不太清楚我的表达式语法哪里出了问题。当我尝试评估维度值逻辑时抛出此错误(阈值逻辑也因类似错误而失败)

InvalidTemplate. Unable to process template language expressions in action 'Post_message' inputs at line '1' and column '1660': 'The template language expression 'last(first('allOf')?['dimensions'])?['value']' cannot be evaluated because property 'dimensions' cannot be selected. Property selection is not supported on values of type 'String'. Please see https://aka.ms/logicexpressions for usage details.'.

JSON 负载

{
    "schemaId": "AzureMonitorMetricAlert",
    "data": {
        "version": "2.0",
        "properties": null,
        "status": "Deactivated",
        "context": {
            "timestamp": "2019-06-11T21:26:20.5035755Z",
            "id": "/URLTEXT/",
            "name": "FBIS Event Bus DLQ Threshold Notifier",
            "description": "",
            "conditionType": "SingleResourceMultipleMetricCriteria",
            "severity": "3",
            "condition": {
                "windowSize": "PT5M",
                "allOf": [
                    {
                        "metricName": "DeadletteredMessages",
                        "metricNamespace": "Microsoft.ServiceBus/namespaces",
                        "operator": "GreaterThan",
                        "threshold": "5",
                        "timeAggregation": "Average",
                        "dimensions": [
                            {
                                "name": "ResourceId",
                                "value": "123456:fbis-event-bus"
                            },
                            {
                                "name": "EntityName",
                                "value": "accountscontacts-account-deleted"
                            }
                        ],
                        "metricValue": 4
                    }
                ]
            },
            "subscriptionId": "1234",
            "resourceGroupName": "SharedResources",
            "resourceName": "FBIS-Event-Bus",
            "resourceType": "Microsoft.ServiceBus/namespaces",
            "resourceId": "/URLTEXT/",
            "portalLink": "PORTALLINK"
        }
    }
}

这是带有表达式块的逻辑应用程序的快照(我想我也不确定我是否也在此处正确访问属性),

知道如何使用表达式语法动态地正确访问这些属性吗?

有两件事需要注意。一个是 When a HTTP request is receivedbody 是字符串格式,它不支持 select 属性 这就是你得到错误的原因。

第二个是你的Json数据包括数组数据,所以当你select数据时你需要添加索引。

因此解决方案是首先使用 Parse JSON 操作将您的数据解析为 json,架构与 When a HTTP request is received 相同。那么你就可以select属性了。我测试了两个属性:thresholddimensions valuethreshold 是这个表达式:body('Parse_JSON')['data']['context']['condition']['allOf'][0]['threshold']dimensions value 是这个表达式:body('Parse_JSON')['data']['context']['condition']['allOf'][0]['dimensions'][1]['value'].