The Map function in Mulesoft is used to iterate over an array of objects or values. In this article, I will explain the usage of the Map function with examples.
The syntax of the Map function is
payload map(value,index) -> { key: value }
Here the index is the position of the object or value in the array starting from zero to n-1 and the value is the complete object. The following JSON array example is used throughout in the examples as input.
[ { "tool" : "mulesoft", "rank" : 1 }, { "tool" : "informatica", "rank" : 2 } ]
The basic usage of a map is iterating over an array of JSON data structure. The following example code iterates over the above JSON array and results in the output the same as the input.
%dw 2.0 output application/json --- payload map (ts, index) -> { tool : ts.tool, rank : ts.rank }
The $$ symbol refers to the index of the object and the $ refers to the object or value. The following example shows the usage of these symbols in the Map function.
%dw 2.0 output application/json --- payload map () -> { ($$) : $ }
This DW code results in the below output.
[ { "0": { "tool": "mulesoft", "rank": 1 } }, { "1": { "tool": "informatica", "rank": 2 } } ]
Here, the $$ symbol generates indexes 0 and 1 as there are only two objects. The $ symbol produces the complete object as you can see in the above result.
The filter function works like a where clause in SQL. It is used to remove unwanted data before passing the JSON to the map function. The usage of a map along with a filter is demonstrated in the following example.
%dw 2.0 output application/json --- payload filter ($.tool == "mulesoft") map (ts, idx) -> { tool : ts.tool, rank : ts.rank }
Here the filter function only picks the object with tool value as mulesoft. It results in the below output.
[ { "tool": "mulesoft", "rank": 1 } ]
We can add new fields and remove existing fields while iterating through the JSON. The following Map code function illustrates this behavior.
%dw 2.0 output application/json --- payload map (ts, idx) -> { tool : ts.tool, priority: "high" }
Here we have removed the rank field and added the priority field. The execution of the code produces the below output.
[ { "tool": "mulesoft", "priority": "high" }, { "tool": "informatica", "priority": "high" } ]
In the earier examples, we have iterated over array of objects. Here, We will iterate over the array of values. This is shown in the below map code.
%dw 2.0 output application/json --- [1,2,3,4,5] map() -> { ($$) : $ }
This example produces output with index and as well as value.
[ { "0": 1 }, { "1": 2 }, { "2": 3 }, { "3": 4 }, { "4": 5 } ]
Lets combine Map and orderBy functions to pre process the data and sort it based on a field. Lets take a look at the below code.
%dw 2.0 output application/json --- ( payload map () -> { tool : $.tool, rank : if ($.rank == 1) "B" else "A" } ) orderBy $.rank
The map function transforms the rank field. If the rank value is 1 then it assigns "B" otherwise "A". Then the orderBy function sorts the data on the rank field. The output of this entire operation is
[ { "tool": "informatica", "rank": "A" }, { "tool": "mulesoft", "rank": "B" } ]
The distinctBy function is used to get unique values from an array. The usage of the distinctBy function is demonstrated in the following example.
%dw 2.0 output application/json --- [1,2,3,2,4,5] distinctBy $
The execution of the above DW code produces the below output.
[ 1, 2, 3, 4, 5 ]
You can use the map function to do some data transformations and then apply the distinctBy function to get unique values.
To conclude, we have seen the usage of Map function with multiple examples along with other mulesoft built-in functions. Hope this article will help you in resolving some of your problems at work.