1
votes

I am new to mule and I am unable to transform this nested array to my required format. I searched various links but couldn't find much help relevant to my requirement. I am attaching the snippet which I have tried till now and I am unable to figure out how to proceed further.

Code

%dw 2.0
output application/json
var myvar={
    "name" : ["Shawn","James","Paul"],
    "sd" : ["2020-12-23","2020-12-24","2020-12-24"],
    "ed" : ["2020-12-25","2020-12-28","2020-12-27"]
}
---
myvar.name zip myvar.sd zip myvar.ed

Required Output

[
    {       
        'name': "shawn",
        'sd': "2020-12-23",
        'ed': "2020-12-25"      
    },
    {       
        'name': "james",
        'sd': "2020-12-24",
        'ed': "2020-12-28"      
    },
    {       
        'name': "Paul",
        'sd': "2020-12-24",
        'ed': "2020-12-27"      
    }   
]

Actual Output

[
  [
    [
      "Shawn",
      "2020-12-23"
    ],
    "2020-12-25"
  ],
  [
    [
      "James",
      "2020-12-24"
    ],
    "2020-12-28"
  ],
  [
    [
      "Paul",
      "2020-12-24"
    ],
    "2020-12-27"
  ]
]

Any sort of guidance or hint or any relevant links would be very helpful to me.

1
do you know any online compiler for code?Daniil Loban
@DaniilLoban what kind of compiler are you expecting?Karthik
Karthik, for run you codeDaniil Loban
You can run your own instance of the playground using the docker image located at hub.docker.com/r/machaval/dw-playgroundSalim Khan
Instructions to setup the same - medium.com/@ramsunka/…Salim Khan

1 Answers

2
votes

Try with this.. Many ways to solve this though!!

%dw 2.0
output application/json
import * from dw::core::Arrays
var myvar={
    "a": ["Shawn", "James", "Paul"],
    "b": ["2020-12-23", "2020-12-24", "2020-12-24"],
    "c": ["2020-12-25", "2020-12-28", "2020-12-27"]
}
---
1 to 3 map 
{
    name: drop(myvar.a,($-1))[0],
    sd: drop(myvar.b,($-1))[0],
    ed: drop(myvar.c,($-1))[0]
}