user12277274
user12277274

Reputation: 109

How to split two arrays into object using Dataweave

I am fetching data from database and trying to assigning using dataweave. However, I see if there are multiple values fetch for a query then it is combining all the values of column into single array.

Currently with the my dataweave logic it is return me this output

Current Output:

"orders": [
        {
          "orderID": "[10355, 10383, 10453]",
          "employeeID": "[6, 8, 1]"
        }
      ]

Expected Output:

"orders": [
            {
              "orderID": "103553",
              "employeeID": "6"
            },
            {
              "orderID": "10383",
              "employeeID": "8"
            },
            {
              "orderID": "10453",
              "employeeID": "1"
            }
          ]

Thanks in advance

Upvotes: 0

Views: 647

Answers (1)

Alex
Alex

Reputation: 4473

You can change String to JSON on the fly and then use it https://simpleflatservice.com/mule4/ChangeStringToJsonOnTheFly.html

%dw 2.0
var x={"orders": [
        {
          "orderID": "[10355, 10383, 10453]",
          "employeeID": "[6, 8, 1]"
        }
      ]    
}
output application/json
---
{
    orders: read(x.orders[0].orderID,'application/json') map (item,index) ->
    {
        orderID:item, 
        "employeeID": read(x.orders[0].employeeID,'application/json')[index]
    }
}

output

{
  "orders": [
    {
      "orderID": 10355,
      "employeeID": 6
    },
    {
      "orderID": 10383,
      "employeeID": 8
    },
    {
      "orderID": 10453,
      "employeeID": 1
    }
  ]
}

Upvotes: 1

Related Questions