Read Array Value Using Dataweave in Mule Read Array Value Using Dataweave in Mule json json

Read Array Value Using Dataweave in Mule


The problem is that the dot selector (.) works on object and on array of objects. When it is applied to an array it will apply the dot selector to all the elements of the array that are of type object and return that result.

Lets go part by part

payload.source

Returns

[  {    "uri": "entities/1R6xV",    "createdBy": "API_USER",    "createdTime": 1562504739146,    "attributes": {      "label": "000000000002659654",      "value": {        "Name": [          {          }        ],        "Id": [          {          }        ],        "Number": [          {            "type": "config/Types/Number/attributes/Number",            "ov": true,            "value": "000000000002659654",            "uri": "entities/1R6xV/attributes/Num/1ZtyT/Number/60pvN6"          }        ]      }    }  }]

So far so good as payload is an Object it returns the value of source that is an array

payload.source.attributes

Returns

[  {    "label": "000000000002659654",    "value": {      "Name": [        {        }      ],      "Id": [        {        }      ],      "Number": [        {          "type": "config/Types/Number/attributes/Number",          "ov": true,          "value": "000000000002659654",          "uri": "entities/1R6xV/attributes/Num/1ZtyT/Number/60pvN6"        }      ]    }  }]

Works ok because the result of payload.source was ended an Array of object so it will do that selection over those objects.

Now when you execute

payload.source.attributes.value.Number

It returns

[  [    {      "type": "config/Types/Number/attributes/Number",      "ov": true,      "value": "000000000002659654",      "uri": "entities/1R6xV/attributes/Num/1ZtyT/Number/60pvN6"    }  ]]

That is an array of arrays and here is where it is broken.

My Solution

You have two alternatives here

  • Use flatten function

    flatten(payload.source.attributes.value.Number).value

  • Use descendant selector

    payload.source.attributes.value.Number..value


Since Number is an array, you need to specify the index you want. In this case, the zeroth element:

Value: payload.source[0].attributes.value.Number[0].value

If you have multiple numbers, it would look something like this:

%dw 1.0%output application/json---values: payload.source[0].attributes.value.Number map {    value: $.value}