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 Free
Surprisingly 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.
From the above payload if we want to filter the data based on type.
Payload.EmployeeDetails.*Data filter($.@'type'=="Teaching Department")
The below dataweave code shows how can we filter the data and the output is in xml.
DepartmentType: payload.EmployeeDetails.*Data filter($.@'type'=="School Teaching Department")
Scenario #2: There might be some requirement where we want to remove some unused data from the payload and use the filter function.
Payload map ($ - 'ServiceDetails') filter ($.'Age'>32)
I hope these examples will help you. I will be happy to answer any questions.
Opinions expressed by DZone contributors are their own.