Share via


Logic Apps - extract value from JSON with nested arrays

Question

Monday, December 10, 2018 9:37 PM

We have a JSON file with 2 nested arrays inside.

JSON example:

{
*    "BusinesUnitId": "f174b943-b9ac-e811-a967-000d3ab204d5",*
*    "CorrelationId": "68a54dbb-3700-462a-8674-ab493746ee8c",   *
*    "PostEntityImages": [*
*        {*
*            "key": "account",*
*            "value": {*
*                "Attributes": [*
*                    {*
*                        "key": "customertypecode",*
*                        "value": {*
*                            "__type": "OptionSetValue:http://schemas.microsoft.com/xrm/2011/Contracts",*
*                            "Value": 285030000*
*                        }*
*                    },*
*                    {*
*                        "key": "address2_addresstypecode",*
*                        "value": {*
*                            "__type": "OptionSetValue:http://schemas.microsoft.com/xrm/2011/Contracts",*
*                            "Value": 1*
*                        }*
*                    },*
*                    {*
*                        "key": "merged",*
*                        "value": false*
*                    },*
*                    {*
***                        "key": "accountnumber",***
***                        "value": "ACC-01003-8ECV5E"***
*                    }*
.....

But unfortunately we fail to extract the accountnumber value.

We tried the following statement, but don't get any result.

"@body('Compose')?['PostEntityImages']?[0]?['value']?['Attributes']?[3]?['value']"

How can we achieve this?

Can we parse the value with one single statement, or do we really have to loop over each array?

All replies (4)

Tuesday, December 11, 2018 6:48 AM | 1 vote

HI,

I was able to get the value with the below expression

@triggerBody()?['PostEntityImages'][0]?['value']?['Attributes'][3]?['value']

Check in your compose you are getting the correct Json as expected

Sujith


Tuesday, December 11, 2018 7:42 AM

HI Sujith

Thanks for your reply.

As a test I just pasted the full JSON in a compose action.

JSON seems to be correct.

But I don't get any result.


Tuesday, December 11, 2018 8:47 AM

Hi,

Change it like below, it should work

@Outputs('Compose')?['PostEntityImages'][0]?['value']?['Attributes'][3]?['value']

it like below

Sujith


Tuesday, December 11, 2018 11:38 AM

Below is code view sample for which accept your input JSON as a payload

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Response": {
                "inputs": {
                    "body": " @{triggerBody()?['body']?['PostEntityImages'][0]?['value']?['Attributes'][3]?['value']}",
                    "statusCode": 200
                },
                "kind": "Http",
                "runAfter": {},
                "type": "Response"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "method": "POST",
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    }
}

Below is the postman request with actual payload:

POST https://prod-XXX.southeastasia.logic.azure.com:443/workflows/c991XXXXXXXXXXXXXf6f34ceaa6/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=5SsJXdpj-XdNFidmu-iNCzCHpE3Z_tH-qUtMsd7A24U; HTTP/1.1
Host: XXX.xxx.southeastasia.logic.azure.com:443
Content-Type: application/json
{
  "body": {
    "BusinesUnitId": "f174b943-b9ac-e811-a967-000d3ab204d5",
    "CorrelationId": "68a54dbb-3700-462a-8674-ab493746ee8c",
    "PostEntityImages": [
      {
        "key": "account",
        "value": {
          "Attributes": [
            {
              "key": "customertypecode",
              "value": {
                "Value": 285030000,
                "__type": "OptionSetValue:http://schemas.microsoft.com/xrm/2011/Contracts"
              }
            },
            {
              "key": "address2_addresstypecode",
              "value": {
                "Value": 1,
                "__type": "OptionSetValue:http://schemas.microsoft.com/xrm/2011/Contracts"
              }
            },
            {
              "key": "merged",
              "value": false
            },
            {
              "key": "accountnumber",
              "value": "ACC-01003-8ECV5E"
            }
          ]
        }
      }
    ]
  }
}