MuleSoft Mule4 Tutorial: How to Use Map & MapObject DataWeave Functions to Iterate on Array of Objects

By | January 20, 2021

MuleSoft dataweave expression language provides a rich set of functions and operators which can be used to perform various types of operations on payload to achieve desired transformations. When implementing integration flows using Mule ESB, we often come across scenarios where we have to deal with a large set of data in the form of array of objects and we are required to iterate through the array of objects and do the necessary transformations on each records in the array or on a single object within the array. This is where map and mapObject functions come handy.

In this post, we will explain difference between map and mapObject in Mule4 and then explain how to use map and mapObject dataweave functions to iterate on array of objects or on an individual object attributes.

What is the Difference between map and mapObject in MuleSoft

For beginners learning MuleSoft Mule4, map vs mapObject is a common question that comes in the mind to understand the difference between the two functions and decide on which of these functions to use in a specific scenario.

Map function is used to iterate on an array of objects and it returns a transformed array of objects as an output after applying any transformations. Map function picks on object from the array at every iteration and it has two arguments containing the current object and index of current object respectively.

Syntax for Map Function in Mule4:

Suppose that we have an array of objects with the name accounts. in this case, if we want to iterate on this accounts objects array, syntax of map function will be as below:

accounts map(account, indexOfAccount) ->
{
// transformation logic goes here
}

You can access value and index using $ and $$ as well for which detailed steps have been explained in the video tutorial that you can find embedded in this post.

On the other hand, mapObject works on a single object and it iterates through the attributes/elements of the object in the form of key value pair with parameters containing value, key and index.

Syntax for MapObject Function in Mule4:

Suppose we have a single account object on which we want to apply mapObject function. For this case, syntax for using mapObject function will be as below:

account mapObject(value, key, indexOfObject) ->
{
// transformation logic goes here
}
You can access value, key and index using $ and $$ and $$$ as well for which detailed steps have been explained in the video tutorial that you can find embedded in this post.

MuleSoft Mule4 Map and MapObject Video Tutorial: How to Use Map and MapObject to Iterate on an Array in DataWeave

Watch below video tutorial on TutorialsPedia YouTube channel where I have explained step by step how to use map and mapObject dataweave functions to do iteration on an array of objects and also I have explained how to use if condition along with map and mapObject to add elements based on certain conditions.

If you have any further questions about this topic, feel free to comment below.

Ajmal Abbasi

Ajmal Hussain Abbasi is Integration Consultant By Profession with 13+ years experience in Integration domain mainly with TIBCO products. He has extensive practical knowledge of TIBCO Business Works, TIBCO Cloud, TIBCO Flogo, TIBCO Mashery, TIBCO Spotfire, EMS and TIBCO ActiveSpaces. He has worked on a number of highly critical integration projects in various sectors by using his skills in TIBCO Flogo, TIBCO API Management (Mashery), TCI, Tibco Designer, TIBCO Business Studio, Adapters, TIBCO EMS, RV, Administrator, TIBCO BE, TIBCO ActiveSpaces etc. Ajmal Abbasi has experience with MuleSoft ESB as well. Ajmal Abbasi is also experienced in the area of API Management particularly with WSO2 API management platforms. Ajmal Abbasi is also experienced in developing solutions using Core Java and J2EE Technologies. You can contact Ajmal Abbasi for Consultancy, Technical Assistance and Technical Discussions.

More Posts - Website - Facebook - LinkedIn - YouTube

One thought on “MuleSoft Mule4 Tutorial: How to Use Map & MapObject DataWeave Functions to Iterate on Array of Objects

  1. Pingback: MuleSoft Mule 4 DataWeave Advanced Tutorial | TutorialsPedia

Leave a Reply

Your email address will not be published. Required fields are marked *