Home > other >  retrieve values from an json array in azure logic app
retrieve values from an json array in azure logic app

Time:09-18

what will be the easiest way to retrive the "HydraulicPressure" and "FailedPickupsLastHr" values from the below json in logic app?

{
  "variables": [
    {
      "name": "sensorarray",
      "type": "String",
      "value": "{\"ContentData\":{\"applicationId\":\"0db2345\",\"deviceId\":\"178\",\"enqueuedTime\":\"2022-09-17T14:27:22.386Z\",\"enrichments\":{},\"messageSource\":\"properties\",\"messageType\":\"devicePropertyReportedChange\",\"properties\":[{\"name\":\"FailedPickupsLastHr\",\"value\":42},{\"name\":\"HydraulicPressure\",\"value\":30.863390107917837}],\"schema\":\"default@v1\",\"templateId\":\"dtmi:z\"},\"Properties\":{\"iotcentral-application-id\":\"05\",\"iotcentral-message-source\":\"properties\",\"iotcentral-message-type\":\"devicePropertyReportedChange\",\"iotcentral-device-id\":\"1q9hn5l4xcl\",\"x-opt-sequence-number\":5663,\"x-opt-offset\":\"5307784\",\"x-opt-enqueued-time\":\"2022-09-17T14:27:28.046Z\",\"message-id\":{\"EncodeSize\":38},\"group-sequence\":0},\"SystemProperties\":{\"EnqueuedTimeUtc\":\"2022-09-17T14:27:28.046Z\",\"Offset\":\"5307784\",\"PartitionKey\":null,\"SequenceNumber\":5663}}"
    }
  ]
}

CodePudding user response:

I have reproduced in my environment, you can similarly do it for your json array as below: Firstly, select initialize variable step and then set it with below json example :

[{
"variables": 
    {
        "name": "sensorarray",
        "type": "String"
    }
}]

Then, select the SELECT step And then select the varaible you want from array and then get it with using item as below:

enter image description here

Output:

enter image description here

enter image description here

Try to update your array as the given example and try the above steps you will get the output as above.

CodePudding user response:

Load this definition as a new LogicApp into your tenant ...

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Initialize_Failed_Pickups_Last_Hr": {
                "inputs": {
                    "variables": [
                        {
                            "name": "FailedPickupsLastHr",
                            "type": "integer",
                            "value": "@variables('Object')['ContentData']['properties'][0]['value']"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Hydraulic_Pressure": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Hydraulic_Pressure": {
                "inputs": {
                    "variables": [
                        {
                            "name": "HydraulicPressure",
                            "type": "string",
                            "value": "@{variables('Object')['ContentData']['properties'][1]['value']}"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Object": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Object": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Object",
                            "type": "object",
                            "value": "@JSON(variables('Payload'))"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Payload": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Payload": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Payload",
                            "type": "string",
                            "value": "{\"ContentData\":{\"applicationId\":\"0db2345\",\"deviceId\":\"178\",\"enqueuedTime\":\"2022-09-17T14:27:22.386Z\",\"enrichments\":{},\"messageSource\":\"properties\",\"messageType\":\"devicePropertyReportedChange\",\"properties\":[{\"name\":\"FailedPickupsLastHr\",\"value\":42},{\"name\":\"HydraulicPressure\",\"value\":30.863390107917837}],\"schema\":\"default@v1\",\"templateId\":\"dtmi:z\"},\"Properties\":{\"iotcentral-application-id\":\"05\",\"iotcentral-message-source\":\"properties\",\"iotcentral-message-type\":\"devicePropertyReportedChange\",\"iotcentral-device-id\":\"1q9hn5l4xcl\",\"x-opt-sequence-number\":5663,\"x-opt-offset\":\"5307784\",\"x-opt-enqueued-time\":\"2022-09-17T14:27:28.046Z\",\"message-id\":{\"EncodeSize\":38},\"group-sequence\":0},\"SystemProperties\":{\"EnqueuedTimeUtc\":\"2022-09-17T14:27:28.046Z\",\"Offset\":\"5307784\",\"PartitionKey\":null,\"SequenceNumber\":5663}}"
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "ParameterTest1": {
                "defaultValue": "\"\"",
                "type": "String"
            }
        },
        "triggers": {
            "manual": {
                "inputs": {
                    "method": "GET",
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

It will give you my answer in full and show you how to get to the values you're after.

The basic steps are ...

  1. Initialize the initial JSON you have as a string.
  2. Then convert that string to an object using the json expression
  3. The last two steps involve expressions to extract the values you're after.

Basically, you need to parse the string value to an object and then evaluate it.

Result

  • Related