-1

Please help with the below transformation

Input :

[{"id":1,"sub":["tamil"]},{"id":2,"sub":["english","maths"]},{"id":3,"sub":["phy","che"]}]

Output:

[
{"id":1,"sub":"tamil"},
{"id":2,"sub":"english"},
{"id":2,"sub":"maths"},
{"id":3,"sub":"phy"},
{"id":3,"sub":"che"}
]```
Bibek Kr. Bazaz
  • 505
  • 10
  • 34

1 Answers1

2

You can use the following DataWeave expression:

%dw 2.0
output application/json
---
flatten(payload map ((item1, index) -> 
    flatten(item1.sub map ((item2, index) -> {
        "id": item1.id,
        "sub": item2
    })
)))
olamiral
  • 1,296
  • 7
  • 8
  • Thanks for the answer. I was having difficulty to get Id value inside nested map. Will remember this trick of item1 and item – Bibek Kr. Bazaz Apr 14 '21 at 12:20
  • You should have mentioned that in your question for context. You can use the default parameters for map(), mapObject(), filter(), etc ($, $$) but for nested mappings it is better to name them to avoid confusions. – aled Apr 14 '21 at 13:04