Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract the inner field objects and apply them to respective top level objects using Dataweave2.0

I am trying to achieve the below output from the given input. I have tried several ways of making a generic function so that when any data is passed with similar structure I get the similar output. Please help me to achieve this.

Input

[{
  "name": "Thinker",
  "details": [
      {
      "num": 1
      }, 
      {
      "num": 2
      }
   ]
}, 
{
  "name": "Blinker",
  "details": [
      {
      "num": 3
      }, 
      {
      "num": 4
      }
   ]
}]

Output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[{
  "name": "Thinker",
  "num": 1
},
{
  "name": "Thinker",
  "num": 2
},
{
  "name": "Blinker",
  "num": 3
},
{
  "name": "Blinker",
  "num": 4
}]

>Solution :

Try this DW

1st Map to Map on main array

2nd Map to Map on details

reduce to convert nested array to single array

%dw 2.0
output application/json
---
payload map(
    ($.details map(item,index)->{
        "name":($.name),
        "num":item[0]
    })
)reduce($$++$)

Output

[
  {
    "name": "Thinker",
    "num": 1
  },
  {
    "name": "Thinker",
    "num": 2
  },
  {
    "name": "Blinker",
    "num": 3
  },
  {
    "name": "Blinker",
    "num": 4
  }
]

DW

%dw 2.0
output application/json
---
payload map(
    ($.details map(item,index)->{
        (keysOf($)[0]):($.name),
        (keysOf(item)[0]):item[0]
    })
)reduce($$++$)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading