Iterate Fixed number of loop in Mule 4 using Dataweave

 

In this tutorial we will demonstrate how we can iterate fixed number of loop in mule 4 using dataweave

Below is the input you can see the size of the array is 6 but we need to iterate the list only 3 times on the basis of the total count

Input

{
 "totalcount": 5,
 "employees":
    [
      {
        "name":"Sam",
        "age":34
      },
       {
        "name":"Richard",
        "score":38
      },
      {
        "name":"Harry",
        "score":36
      },
       {
        "name":"Tom",
        "score":40
      },
      {
        "name":"David",
        "score":84
      },
      {
        "name":"Chris",
        "score":52
      }
    ]
}

 

Dataweave

%dw 2.0
output application/json
---
(payload.employees  map (()->{
FirstName: $.name,
age: $.age
})) [ 0 to payload.totalcount -1 ]

Output:

[
{
"FirstName": "Sam",
"age": 34
},
{
"FirstName": "Richard",
"age": 38
},
{
"FirstName": "Harry",
"age": 36
}
]
  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments