Search code examples
muledataweavemulesoftmule-esb

Nested array data transformation with Mule 3


I am trying to transform data in Mule 3 (DataWeave 1.0) but I am not getting the desire result.

This is the input payload:

[
  "https://scrum-caller.com/repo/v1/inside#Changes",
  [
    {
      "id": "8db55441-6255-4d24-8d39-658536985214",
      "number": "0w-30",
      "Desc": "maintain"
    }
  ],
  "https://scrum-caller.com/repo/v1/inside#Changes",
  [
    {
      "id": "11111111-6666-2222-3g3g-854712547412",
      "number": "5w-40",
      "Desc": "on prod"
    }
  ],
  "https://scrum-caller.com/repo/v1/inside#Changes",
  [
    {
      "id": "1ab32c5b-ffs3-3243-74fv-3376218042bb",
      "number": "5w-30",
      "Desc": "on test"
    }
  ]
]

And my desire output need to be like the one below

{
    "@odata.context": "https://scrum-caller.com/repo/v1/inside#Changes",
    "value": [
        {
      "id": "8db55441-6255-4d24-8d39-658536985214",
      "number": "0w-30",
      "Desc": "maintain"
    },
    {
      "id": "11111111-6666-2222-3g3g-854712547412",
      "number": "5w-40",
      "Desc": "on prod"
    },
    {
      "id": "1ab32c5b-ffs3-3243-74fv-3376218042bb",
      "number": "5w-30",
      "Desc": "on test"
    }
    ]
}

Thanks for helping guys.


Solution

  • Assuming all the URLs are the same, since the question doesn't provide details only an example, I just take the first element as the value of the input for "@odata.context", the the value is just filtering out the non-array elements and use the reduce operator to get a single array of the other elements.

    %dw 1.0
    %output application/json
    ---
    {
      "@odata.context" : payload[0],
      value : payload filter ($ is :array) reduce ($ ++ $$)
    }