Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've got a Logic App I am working with in Azure where I need to access a certain nested value from some JSON on an Http Trigger.

I need to access the threshold value (5) and the value of the second item in the dimensions array (accountscontacts-account-deleted) and display these dynamically through the Expression syntax in the Logic App editor but can't figure out where I am going wrong with my expression.

I'm trying to use this syntax to access the threshold value,

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

and I'm attempting to use this expression to access the second dimension value,

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

Neither of these seem to be working and I can't quite figure out where my Expression syntax is going wrong. It throws this error when I try to evaluate that the dimensions value logic (The threshold logic also fails with a similar error)

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 Payload

{
    "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"
        }
    }
}

Here is a snapshot of what the logic app looks like with the expression block (I guess I am also unsure if I am accessing the properties correctly here too),

Expression Syntax Failure

Any idea how I can correctly access these properties dynamically using the expression syntax?

like image 288
tokyo0709 Avatar asked Jun 11 '19 23:06

tokyo0709


1 Answers

There are two things to be care for. One is the body of When a HTTP request is received is in string format, it doesn't support select property that's why you get the error.

The second is your Json data includes array data, so when you select data you need add index.

So the solution is firstly parse your data into json with Parse JSON action, the schema is same as the When a HTTP request is received. Then you will be able to select property. I test with two properties: threshold and dimensions value. threshold is this expression:body('Parse_JSON')['data']['context']['condition']['allOf'][0]['threshold'] and the dimensions value is this one: body('Parse_JSON')['data']['context']['condition']['allOf'][0]['dimensions'][1]['value'].

enter image description here

enter image description here

like image 176
George Chen Avatar answered Sep 17 '22 00:09

George Chen