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.
Pingback: MuleSoft Mule 4 DataWeave Advanced Tutorial | TutorialsPedia