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 FreeIntroduction
The 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.
The filter operator iterates over an array and applies an expression that returns matching values.
The 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.
The 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.
%dw 2.0
output application/json
var requestBody= ["James","Anthony","Kevin"]
---
requestBody map {
($$):$
}
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:
xxxxxxxxxx
[
{
"0": "James"
},
{
"1": "Anthony"
},
{
"2": "Kevin"
}
]
We will be going through various examples of the map and mapObject operator.
Example 1
We have an array of items which contains employee details like firstName, lastName, age, and salary and want to concatenate the firstName and 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 firstName and lastName as shown in the below Dataweave transformation.
xxxxxxxxxx
%dw 2.0
output application/json
var requestBody=
[{
"firstName": "James",
"lastName": "Peter",
"age":30,
"salary":30000
},
{
"firstName": "Peter",
"lastName": "Gonsalves",
"age":28,
"salary":60000
},
{
"firstName": "Anthony",
"lastName": "Watson",
"age":27,
"salary":50000
}]
---
requestBody map {
name: $.firstName ++ " "++ $.lastName,
age: $.age,
salary:$.salary
}
Output:
xxxxxxxxxx
[
{
"name": "James Peter",
"age": 30,
"salary": 30000
},
{
"name": "Peter Gonsalves",
"age": 28,
"salary": 60000
},
{
"name": "Anthony Watson",
"age": 27,
"salary": 50000
}
]
We can also use lambda expressions with the map operator to achieve the same thing.
xxxxxxxxxx
%dw 2.0
output application/json
var requestBody=
[{
"firstName": "James",
"lastName": "Peter",
"age":30,
"salary":30000
},
{
"firstName": "Peter",
"lastName": "Gonsalves",
"age":28,
"salary":60000
},
{
"firstName": "Anthony",
"lastName": "Watson",
"age":27,
"salary":50000
}]
---
requestBody map(items,index) -> {
name: items.firstName ++ " "++ items.lastName,
age: items.age,
salary:items.salary
}
Example 2
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 firstName and lastName in the final output.
x
%dw 2.0
output application/json
var requestBody=
[{
"firstName": "James",
"lastName": "Peter",
"age":30,
"salary":30000
},
{
"firstName": "Peter",
"lastName": "Gonsalves",
"age":28,
"salary":60000
},
{
"firstName": "Anthony",
"lastName": "Watson",
"age":27,
"salary":50000
}]
---
requestBody filter $.salary > 40000 map(items,index) -> {
name: items.firstName ++ " "++ items.lastName,
age: items.age,
salary:items.salary
}
Output:
xxxxxxxxxx
[
{
"name": "Peter Gonsalves",
"age": 28,
"salary": 60000
},
{
"name": "Anthony Watson",
"age": 27,
"salary": 50000
}
]
Example 3
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 map operator.
In the mapObject operator, $ gives the value, $$ gives the key, and $$$ gives the indices.
x
%dw 2.0
output application/json
var requestBody=
[{
"firstName": "James",
"lastName": "Peter",
"age":30,
"salary":30000
},
{
"firstName": "Peter",
"lastName": "Gonsalves",
"age":28,
"salary":60000
},
{
"firstName": "Anthony",
"lastName": "Watson",
"age":27,
"salary":50000
}]
---
requestBody map (items,index) ->
(
items mapObject(
(upper($$)):$
)
)
Output:
xxxxxxxxxx
[
{
"FIRSTNAME": "James",
"LASTNAME": "Peter",
"AGE": 30,
"SALARY": 30000
},
{
"FIRSTNAME": "Peter",
"LASTNAME": "Gonsalves",
"AGE": 28,
"SALARY": 60000
},
{
"FIRSTNAME": "Anthony",
"LASTNAME": "Watson",
"AGE": 27,
"SALARY": 50000
}
]
Example 4
We want the list of employees whose salary is greater than $40,000 and we want only age and salary in the final output. We can achieve this using multiple approaches.
x
%dw 2.0
output application/json
var requestBody=
[{
"firstName": "James",
"lastName": "Peter",
"age":30,
"salary":30000
},
{
"firstName": "Peter",
"lastName": "Gonsalves",
"age":28,
"salary":60000
},
{
"firstName": "Anthony",
"lastName": "Watson",
"age":27,
"salary":50000
}]
---
requestBody filter $.salary > 40000 map (items,index) ->
{
age: items.age,
salary: items.salary
}
Here's another way to use the filterObject operator:
x
%dw 2.0
output application/json
var requestBody=
[{
"firstName": "James",
"lastName": "Peter",
"age":30,
"salary":30000
},
{
"firstName": "Peter",
"lastName": "Gonsalves",
"age":28,
"salary":60000
},
{
"firstName": "Anthony",
"lastName": "Watson",
"age":27,
"salary":50000
}]
---
requestBody filter $.salary > 40000 map (items,index) ->
(
items filterObject $$$ > 1
)
Output:
xxxxxxxxxx
[
{
"age": 28,
"salary": 60000
},
{
"age": 27,
"salary": 50000
}
]
Example 5
We want to add the "STATUS":"Active" key value pair between lastName and age in each employee item in the array and also display those employees whose salary is less than $40,000 with all keys uppercased.
x
%dw 2.0
output application/json
var requestBody=
[{
"firstName": "James",
"lastName": "Peter",
"age":30,
"salary":30000
},
{
"firstName": "Peter",
"lastName": "Gonsalves",
"age":28,
"salary":60000
},
{
"firstName": "Anthony",
"lastName": "Watson",
"age":27,
"salary":50000
}]
---
requestBody filter $.salary < 40000 map (items,index) ->(
items mapObject (
if($$ as String == "lastName")
{
(upper($$)):$,
"STATUS":"Active"
}
else
{
(upper($$)):$
}
)
)
Output:
xxxxxxxxxx
[
{
"FIRSTNAME": "James",
"LASTNAME": "Peter",
"STATUS": "Active",
"AGE": 30,
"SALARY": 30000
}
]
Difference Between Map and MapOperator Objects
Mapbasically works on the array and the output will always be an array, whereasMapObjectworks 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 whereasFilterObjectcan be used with theMapObjectoperator 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 Map, Filter, MapObject, and FilterObject with MuleSoft Dataweave transformations.
Opinions expressed by DZone contributors are their own.
Comments