How to work with variable from nested JSON data in logic app

Anonymous
2023-06-17T18:12:20.84+00:00

Hello,

I have created a logic app that is triggered by an event hub that receives JSON messages. I parse these messages in the logic app using the Parse JSON action and I also wanted to use a condition to split the messages from one sensor and the other sensor. The JSON messages are the same except for the sensor data and the nAdr variable which specifies the address. I can use this nAdr variable in the condition so that the data for the first sensor is stored somewhere else than the second sensor. Unfortunately, this variable is always set to zero. I checked all the settings. I checked the schema, variable type and other things but the nAdr variable still shows up as null. How do I fix this error? I have nested JSON data and I would need to work with it easily as offered by the logic app. Why is the nAdr variable zero when it always has a value of 1 or 2 in the JSON message?

Here is my JSON message:

{
    "mType": "iqrfSensor_ReadSensorsWithTypes",
    "data": {
        "msgId": "testEmbedSensor",
        "rsp": {
            "nAdr": 1,
            "hwpId": 2,
            "rCode": 0,
            "dpaVal": 88,
            "result": {
                "sensors": [
                    {
                        "id": "TEMPERATURE",
                        "type": 1,
                        "name": "Temperature",
                        "shortName": "t",
                        "value": 26,
                        "unit": "°C",
                        "decimalPlaces": 4
                    },
                    {
                        "id": "TEMPERATURE",
                        "type": 1,
                        "name": "Temperature",
                        "shortName": "t",
                        "value": 24.5,
                        "unit": "°C",
                        "decimalPlaces": 4
                    },
                    {
                        "id": "BINARYDATA7",
                        "type": 129,
                        "name": "Binary data7",
                        "shortName": "bin7",
                        "value": 81,
                        "unit": "?",
                        "decimalPlaces": 0,
                        "breakdown": [
                            {
                                "id": "BINARYDATA7",
                                "type": 129,
                                "name": "Light indicator",
                                "shortName": "light",
                                "unit": "%",
                                "decimalPlaces": 1,
                                "value": 36.2
                            }
                        ]
                    },
                    {
                        "id": "BINARYDATA7",
                        "type": 129,
                        "name": "Binary data7",
                        "shortName": "bin7",
                        "value": 73,
                        "unit": "?",
                        "decimalPlaces": 0,
                        "breakdown": [
                            {
                                "id": "BINARYDATA7",
                                "type": 129,
                                "name": "Potentiometer",
                                "shortName": "pot",
                                "unit": "%",
                                "decimalPlaces": 1,
                                "value": 42.5
                            }
                        ]
                    }
                ],
                "sensorIndexes": [
                    0,
                    1,
                    2,
                    3
                ]
            }
        },
        "raw": [
            {
                "request": "01.00.5e.01.ff.ff.0f.00.00.00",
                "requestTs": "2023-03-20T15:58:18.066+01:00",
                "confirmation": "01.00.5e.01.ff.ff.ff.58.01.08.01",
                "confirmationTs": "2023-03-20T15:58:18.091+01:00",
                "response": "01.00.5e.81.02.00.00.58.01.a0.01.01.88.01.81.51.81.49",
                "responseTs": "2023-03-20T15:58:18.346+01:00"
            }
        ],
        "insId": "iqrfgd2-default",
        "statusStr": "ok",
        "status": 0
    }
}
Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
3,542 questions
Azure Event Hubs
Azure Event Hubs
An Azure real-time data ingestion service.
719 questions
{count} votes

Accepted answer
  1. Alistair Ross 7,466 Reputation points Microsoft Employee
    2023-06-19T18:13:04.7266667+00:00

    Hello @Anonymous

    I've converted my original answer to a comment to not confuse the answers. I've managed to recreate the exact same issue you have been experiencing. I have made a version of the logic app which performs 4 actions.

    1. Get's the value from the event hub (When events are available in Event Hub)
    2. Initialize Variable
    3. Parse JSON

    Set Variable

    I test like this when I am having issues with parsing data and confirming the output. Your version of "Parse JSON" had the trigger body "@triggerBody()" as the input, however I was noticing that when generating the schema for the entire payload, it was not working as expected. Changing this to just the ContentData "@triggerBody()?['ContentData']", there were no issues with the parsed data and the variable is set. Once thing I did notice, was that I had to manually change the schema as the property Value "body('Parse_JSON')?['data']?['rsp']?['result']?['sensors'][ ]?['value']" automatically sets as an integer based on the data you gave me, and it needed to be changed to a number.

    Try my code here below. If you struggle with it. Create your logic app with the event hub trigger and then copy the Parse_JSON from the code:

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "For_each": {
                    "actions": {
                        "Set_variable": {
                            "inputs": {
                                "name": "nadr",
                                "value": "@{items('For_each')?['value']}"
                            },
                            "runAfter": {},
                            "type": "SetVariable"
                        }
                    },
                    "foreach": "@body('Parse_JSON')?['data']?['rsp']?['result']?['sensors']",
                    "runAfter": {
                        "Parse_JSON": [
                            "Succeeded"
                        ]
                    },
                    "type": "Foreach"
                },
                "Initialize_variable": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "nadr",
                                "type": "string"
                            }
                        ]
                    },
                    "runAfter": {},
                    "type": "InitializeVariable"
                },
                "Parse_JSON": {
                    "inputs": {
                        "content": "@triggerBody()?['ContentData']",
                        "schema": {
                            "properties": {
                                "data": {
                                    "properties": {
                                        "insId": {
                                            "type": "string"
                                        },
                                        "msgId": {
                                            "type": "string"
                                        },
                                        "raw": {
                                            "items": {
                                                "properties": {
                                                    "confirmation": {
                                                        "type": "string"
                                                    },
                                                    "confirmationTs": {
                                                        "type": "string"
                                                    },
                                                    "request": {
                                                        "type": "string"
                                                    },
                                                    "requestTs": {
                                                        "type": "string"
                                                    },
                                                    "response": {
                                                        "type": "string"
                                                    },
                                                    "responseTs": {
                                                        "type": "string"
                                                    }
                                                },
                                                "required": [
                                                    "request",
                                                    "requestTs",
                                                    "confirmation",
                                                    "confirmationTs",
                                                    "response",
                                                    "responseTs"
                                                ],
                                                "type": "object"
                                            },
                                            "type": "array"
                                        },
                                        "rsp": {
                                            "properties": {
                                                "dpaVal": {
                                                    "type": "integer"
                                                },
                                                "hwpId": {
                                                    "type": "integer"
                                                },
                                                "nAdr": {
                                                    "type": "integer"
                                                },
                                                "rCode": {
                                                    "type": "integer"
                                                },
                                                "result": {
                                                    "properties": {
                                                        "sensorIndexes": {
                                                            "items": {
                                                                "type": "integer"
                                                            },
                                                            "type": "array"
                                                        },
                                                        "sensors": {
                                                            "items": {
                                                                "properties": {
                                                                    "breakdown": {
                                                                        "items": {
                                                                            "properties": {
                                                                                "decimalPlaces": {
                                                                                    "type": "integer"
                                                                                },
                                                                                "id": {
                                                                                    "type": "string"
                                                                                },
                                                                                "name": {
                                                                                    "type": "string"
                                                                                },
                                                                                "shortName": {
                                                                                    "type": "string"
                                                                                },
                                                                                "type": {
                                                                                    "type": "integer"
                                                                                },
                                                                                "unit": {
                                                                                    "type": "string"
                                                                                },
                                                                                "value": {
                                                                                    "type": "number"
                                                                                }
                                                                            },
                                                                            "required": [
                                                                                "id",
                                                                                "type",
                                                                                "name",
                                                                                "shortName",
                                                                                "unit",
                                                                                "decimalPlaces",
                                                                                "value"
                                                                            ],
                                                                            "type": "object"
                                                                        },
                                                                        "type": "array"
                                                                    },
                                                                    "decimalPlaces": {
                                                                        "type": "integer"
                                                                    },
                                                                    "id": {
                                                                        "type": "string"
                                                                    },
                                                                    "name": {
                                                                        "type": "string"
                                                                    },
                                                                    "shortName": {
                                                                        "type": "string"
                                                                    },
                                                                    "type": {
                                                                        "type": "integer"
                                                                    },
                                                                    "unit": {
                                                                        "type": "string"
                                                                    },
                                                                    "value": {
                                                                        "type": "number"
                                                                    }
                                                                },
                                                                "required": [
                                                                    "id",
                                                                    "type",
                                                                    "name",
                                                                    "shortName",
                                                                    "value",
                                                                    "unit",
                                                                    "decimalPlaces"
                                                                ],
                                                                "type": "object"
                                                            },
                                                            "type": "array"
                                                        }
                                                    },
                                                    "type": "object"
                                                }
                                            },
                                            "type": "object"
                                        },
                                        "status": {
                                            "type": "integer"
                                        },
                                        "statusStr": {
                                            "type": "string"
                                        }
                                    },
                                    "type": "object"
                                },
                                "mType": {
                                    "type": "string"
                                }
                            },
                            "type": "object"
                        }
                    },
                    "runAfter": {
                        "Initialize_variable": [
                            "Succeeded"
                        ]
                    },
                    "type": "ParseJson"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {
                "$connections": {
                    "defaultValue": {},
                    "type": "Object"
                }
            },
            "triggers": {
                "When_events_are_available_in_Event_Hub": {
                    "evaluatedRecurrence": {
                        "frequency": "Minute",
                        "interval": 5
                    },
                    "inputs": {
                        "host": {
                            "connection": {
                                "name": "@parameters('$connections')['eventhubs']['connectionId']"
                            }
                        },
                        "method": "get",
                        "path": "/@{encodeURIComponent('q1309140')}/events/batch/head",
                        "queries": {
                            "consumerGroupName": "$Default",
                            "contentType": "application/json",
                            "maximumEventsCount": 50
                        }
                    },
                    "recurrence": {
                        "frequency": "Minute",
                        "interval": 5
                    },
                    "splitOn": "@triggerBody()",
                    "type": "ApiConnection"
                }
            }
        },
        "parameters": {
            "$connections": {
                "value": {
                    "eventhubs": {
                        "connectionId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/Q1309140/providers/Microsoft.Web/connections/eventhubs",
                        "connectionName": "eventhubs",
                        "connectionProperties": {
                            "authentication": {
                                "type": "ManagedServiceIdentity"
                            }
                        },
                        "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/providers/Microsoft.Web/locations/westeurope/managedApis/eventhubs"
                    }
                }
            }
        }
    }
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-06-19T10:07:29.09+00:00

    I'm trying to store the entire JSON message in blob storage. But the messages are sent from two sensors, so I want to split them by nAdr into two different blob storage.
    Here are screenshots of my attempt to build a logic app:
    User's image

    User's image

    User's image


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.