Dataweave Filter Function
I wanted to take this opportunity to share my experience with filters with you — how can we use the filter function in some complex scenarios.
Join the DZone community and get the full member experience.
Join For FreeSurprisingly there are very few examples of the filter function of Dataweave in MuleSoft. I wanted to take this opportunity to share my experience with filters with you. The following example shows how can we use the filter function in some complex scenarios.
The filter function in the dataweave is used to filter the values based on matching expression. The expression returns the boolean values either true or false. This function can be applied to an array. It iterates the array and matches the values with the expression. The output of the filter function is an array with the matching values.
Scenario #1: Here the example shows how to filter an xml based on arguments of an element. The below dataweave code shows how can we achieve this if the output is in JSON.
Input:
<?xml version="1.0" encoding="UTF-8"?>
<EmployeeDetails>
<Data type="Accountant Department">
<Employee>
<Name>John</Name>
<Designation>Accountant</Designation>
</Employee>
<Employee>
<Name>Kevin</Name>
<Designation>Consultant</Designation>
</Employee>
</Data>
<Data type="Teaching Department">
<Employee>
<Name>Jolly</Name>
<Designation>Science Teacher</Designation>
</Employee>
<Employee>
<Name>Chrissy</Name>
<Designation>Maths Teacher</Designation>
</Employee>
</Data>
</EmployeeDetails>
From the above payload if we want to filter the data based on type.
DataWeave Code:
%dw 2.0
Output application/json
Payload.EmployeeDetails.*Data filter($.@'type'=="Teaching Department")
Output:
xxxxxxxxxx
[
{
"Employee": {
"Name": "Jolly",
"Designation": "Science Teacher"
},
"Employee": {
"Name": "Chrissy",
"Designation": "Maths Teacher"
}
}
]
The below dataweave code shows how can we filter the data and the output is in xml.
DataWeave Code:
%dw 2.0
Output application/xml
DepartmentType: payload.EmployeeDetails.*Data filter($.@'type'=="School Teaching Department")
Output:
xxxxxxxxxx
<?xml version='1.0' encoding='UTF-8'?>
<DepartmentType>
<Employee>
<Name>Jolly</Name>
<Designation>Science Teacher</Designation>
</Employee>
<Employee>
<Name>Chrissy</Name>
<Designation>Maths Teacher</Designation>
</Employee>
</DepartmentType>
Scenario #2: There might be some requirement where we want to remove some unused data from the payload and use the filter function.
Input:
xxxxxxxxxx
[
{
"EmpName": "John",
"Age": "32",
"ServiceDetails":{
"Designation": "Clerk",
"DOJ": "Sept 20 1998"
}
},
{
"EmpName": "Kelvin",
"Age": "33",
"ServiceDetails":{
"Designation": "Accountant",
"DOJ": "Sept 20 2015"
}
}
]
DataWeave Code:
%dw 2.0
Output application/json
Payload map ($ - 'ServiceDetails') filter ($.'Age'>32)
Output:
xxxxxxxxxx
[
{
"EmpName": "Kelvin",
"Age": "33"
}
]
I hope these examples will help you. I will be happy to answer any questions.
Opinions expressed by DZone contributors are their own.
Comments