maomifadacai
maomifadacai

Reputation: 369

how to flat subarray into array in dataweave

i have this input

[{
    "omsNo": "S001",
    "recipient": [{
            "name": "name1",
            "address": "address1"
        },
        {
            "name": "name2",
            "address": "address2"
        }
    ]
}]

may i know how to get this result using dataweave

[{"omsNo":"S001","name":"name1","address":"address1"},{"omsNo":"S001","name":"name2","address":"address2"}]

Upvotes: 0

Views: 509

Answers (2)

TheOtherGuy
TheOtherGuy

Reputation: 184

Here's an alternative solution:

%dw 2.0
output application/json
---
payload.recipient[0] map (v0,k0) ->
{
    omsNo:payload.omsNo[0],
    name:v0.name,
    address:v0.address
}

Upvotes: 0

Salim Khan
Salim Khan

Reputation: 4303

Input

[{
    "omsNo": "S001",
    "recipient": [{
            "name": "name1",
            "address": "address1"
        },
        {
            "name": "name2",
            "address": "address2"
        }
    ]
}]

Script

%dw 2.0
output application/json
---
flatten(payload map ((item, index) -> 
   (item.recipient map ((itemRecipient, indexRecipient)  -> (
        ({omsNo: item.omsNo} ++ itemRecipient) 
   ))
)))

Output

[
  {
    "omsNo": "S001",
    "name": "name1",
    "address": "address1"
  },
  {
    "omsNo": "S001",
    "name": "name2",
    "address": "address2"
  }
]

Upvotes: 3

Related Questions