1

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.

8
  • do you know any online compiler for code? Commented Dec 26, 2020 at 12:31
  • @DaniilLoban what kind of compiler are you expecting? Commented Dec 26, 2020 at 12:43
  • Karthik, for run you code Commented Dec 26, 2020 at 12:45
  • You can run your own instance of the playground using the docker image located at hub.docker.com/r/machaval/dw-playground Commented Dec 26, 2020 at 12:52
  • Instructions to setup the same - medium.com/@ramsunka/… Commented Dec 26, 2020 at 12:53

1 Answer 1

2

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]
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you @SalimKhan .That worked well...Can you just explain shortly how this works? I am kind of new to this
Sure.. I am trying to create a for loop of sorts using 1 to 3 or i could do 1 to sizeOf(myvar.a) (if the size of the three arrays is always going to be the same). After that its about building the object that is needed. Drop is core arrays function (docs.mulesoft.com/mule-runtime/4.3/dw-arrays-functions-drop) that drops the first n elements specified in the function defintion - drop (array,n) and returns an array with the rest of the elements. So in each of the iterations i keep picking the first element from the remainder array.
In first iteration (when $ =1 , since its starting from 1) it would have dropped none, and thus [0]th element would actually give the first required set of "Shawn" ,"2020-12-23","2020-12-25" . Next when $ = 2 it drops Shawn and keeps James and Paul and [0]th element there would give me James and the remainder of the fields for sd and ed. Similarly when $ is 3 , it would have dropped the first two elements and what remains is details for Paul which i can pick using the [0]th element, since the drop function always returns an array. HTH.
Thanks @SalimKhan...That was very much helpful.. But if size of array varies someday, in that case what should i do? Like now if the each array has 5 elements in that case what should I do?
1 to sizeOf(myvar.a)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.