DataWeave Exercise: Usage of Filter Function
In this DataWeave exercise, learn more about how to filter an array using DataWeave's core module function filter.
Join the DZone community and get the full member experience.
Join For FreeIn this blog, I'll demonstrate how to use DataWeave filter functions in various ways.
Input:
All the examples below use the same input.
[
{
"id": "123",
"name": "John",
"salary": 10000,
"age": 61
},
{
"id": "456",
"name": "Charles",
"salary": 20000,
"age": 40
},
{
"id": "789",
"name": "Paul",
"salary": 30000,
"age": 30
},
{
"id": "101",
"name": "George",
"salary": 40000,
"age": 62
}
]
Example 1
Desired Output:
Filter the given Input using the condition age is less than 60 to get the output shown below.
[
{
"id": "456",
"name": "Charles",
"salary": 20000,
"age": 40
},
{
"id": "789",
"name": "Paul",
"salary": 30000,
"age": 30
}
]
The above output can be obtained in a number of ways.
Using Filter:
%dw 2.0
output application/json
---
payload filter $.age < 60
The input array is filtered in the above code using the filter function of the core module, where '$' refers to each object in the array, '$.age' refers to the input object's "age" field, and '$.age < 60' is the filter condition.
Using Filter Expression (Selector):
%dw 2.0
output application/json
---
payload [?($.age < 60)]
Instead of writing a filter function, the array is filtered using a filter expression, which requires square brackets, a question mark, and a filter condition inside the parenthesis.
Using Filter With Map Functions:
%dw 2.0
output application/json
---
payload filter $.age < 60 map {
"id" : $.id,
"name" : $.name,
"Salary" : $.salary,
"age" : $.age
}
The above code applies the filter to the input payload and then uses the map function. The same can be written in another way as below.
%dw 2.0
output application/json
---
payload map {
"id" : $.id,
"name" : $.name,
"Salary" : $.salary,
"age" : $.age
} filter $.age < 60
The map function is applied to the input payload, and then the filter is applied to the result of the map.
Using Filter Expression With Map Function:
%dw 2.0
output application/json
---
payload [?($.age < 60)] map {
"id" : $.id,
"name" : $.name,
"Salary" : $.salary,
"age" : $.age
}
It's similar to using the filter function, but filter expression is used instead.
Example 2
Desired Output:
Filter the given Input using the condition age is less than 60 to get the output shown below.
[
{
"name": "Charles",
"Salary": 20000,
"id": "456"
},
{
"name": "Paul",
"Salary": 30000,
"id": "789"
}
]
Using Filter With Map:
%dw 2.0
output application/json
---
payload filter $.age < 60 map {
"id" : $.id,
"name" : $.name,
"Salary" : $.salary,
}
After filtering the input array (payload), the map was used. It can't be done the other way around, as shown below. This won't work because the filter is applied to the map's output, which doesn't have an age field, hence, why it won't work in this case.
%dw 2.0
output application/json
---
payload map {
"id" : $.id,
"name" : $.name,
"Salary" : $.salary,
} filter $.age < 60
Using Filter With Map and “-” Operator:
%dw 2.0
output application/json
---
payload filter $.age < 60 map {
($)
} - "age"
Here, the filter is applied on a given input array with the map function. Since the "age" field is not required in the output, "-" is used to remove it.
Using Filter Expression With Map Function:
%dw 2.0
output application/json
---
payload [?($.age < 60)] map {
"id" : $.id,
"name" : $.name,
"Salary" : $.salary,
}
Instead of the filter function used, the expression is used.
Opinions expressed by DZone contributors are their own.
Comments