Deep Dive Into DataWeave Map, Filter, MapObject, and Filter Object Operator
A developer and DZone Core member gives a tutorial on using different operators in order to work with arrays in MuleSoft's Dataweave platform.
Join the DZone community and get the full member experience.Join For Free
map operator is a function in Dataweave which iterates over the items in an array and outputs them into a new array. It basically accepts input as a list of items in an array and manipulates the items in the array in order to form a new array as an output.
filter operator iterates over an array and applies an expression that returns matching values.
mapObject operator is a function in Dataweave which iterates over the object using the mapper that acts on keys, values, or indices of that object.
filterObject operator iterates over a list of key-value pairs in an object and applies an expression that returns only matching objects, filtering out the rest from the output.
Let's say we have the array
["James","Anthony","Kevin"], which contains three items and needs the output as index of the item and its value at that particular index.
In the above example,
$$ will give the index of the item and
$ will give the actual value at that index. So the output of the above Dataweave code will be as follows:
We will be going through various examples of the map and
We have an array of items which contains employee details like
salary and want to concatenate the
lastName in the final output. To achieve this, we will be using the
map operator to iterate over each employee object in the array and concatenate the
lastName as shown in the below Dataweave transformation.
We can also use lambda expressions with the
map operator to achieve the same thing.
We will see how the
filter operator can be used with the
map operator. We want a list of employees whose salary is greater than $40,000 and to also concatenate
lastName in the final output.
We want to convert all the keys to uppercase in the array of items. We can iterate over the array of items using the
map operator and to manipulate the keys in the object we can use the
mapObject operator, as this operator can act on the keys, unlike the
$ gives the value,
$$ gives the key, and
$$$ gives the indices.
We want the list of employees whose salary is greater than $40,000 and we want only
salary in the final output. We can achieve this using multiple approaches.
Here's another way to use the
We want to add the
"STATUS":"Active" key value pair between
age in each employee item in the array and also display those employees whose salary is less than $40,000 with all keys uppercased.
Difference Between Map and MapOperator Objects
Mapbasically works on the array and the output will always be an array, whereas
MapObjectworks on single items or objects and the output will always be an object.
Mapprovides the value and index: value can be accessed using
$, index can be accessed using
MapObjectprovides the value, key, and index: the value can be accessed using
$, the key can be accessed using
$$, and the index can be accessed using
- Filter can be used with the
Mapoperator or an array of items whereas
FilterObjectcan be used with the
MapObjectoperator or object.
DataWeave 2.0 Map Operator and Filter With MuleSoft
DataWeave Transformation (Map and MapObject Operator) With MuleSoft
DataWeave Transformation (Filter and FilterObject) With MuleSoft
Now you know how to use
FilterObject with MuleSoft Dataweave transformations.
Opinions expressed by DZone contributors are their own.