Filtering out unwanted values is a must when writing data transformations and DataWeave can do this with ease. This post shows you how to filter values in an array using DataWeave 2.0’s filter function and filter selector.
Quick Navigation
What is DataWeave’s filter function?
filter() is a function in the dw::core library that takes an array and filters out values NOT met by the condition. The function will return a new array with values that are met by the filter condition.
One possible use case for the filter function is to filter out unwanted response data from a downstream API in a user experience API for a mobile application. You can write Dataweave expressions using the filter function in the following ways:
Infix Notation: Long-Hand
[ ] filter (value, index) -> (condition)
Infix Notation: $ syntax
[ ] filter (<condition using $, $$>)
Note: $ represents the current value of an array, while $$ represent the current index of the array. You can use the $ syntax inside your lambda expression.
Prefix Notation
filter([ ], (value, index) -> (condition))
Note: Functions in the dw::core library do not require you to import them. It is already made available for your dw scripts.
What is DataWeave’s Filter expression?
DataWeave also provides the filter selector as a way to filter array values.
Filter Expression
[ ] [? (<boolean_expression>) ]
Note: Unlike the filter function, this expression also works on objects.
Fitler Examples
Example 1: Filter by value
%dw 2.0
output application/json
---
["AAPL", "MSFT", "NVDA", "TSLA", "CRM", "GOOG", "GOOGL"]
filter
(value) -> (value contains "GO")
This example passes into the filter function an array (before the function keyword filter) and a lambda expression after the keyword filter. It returns a new array with values that contain the string “GO” from the unfiltered array.
Example 2: Filter by index
%dw 2.0
output application/json
---
["AAPL", "MSFT", "NVDA", "TSLA", "CRM", "GOOG", "GOOGL"]
filter
(value, index) -> ((index mod 2) == 0)
This example passes into the filter function an array (before the function keyword filter) and a lambda expression after the keyword filter. It returns a new array with values based on the even index in the unfiltered array.
Example 3: Filtering with $ syntax
%dw 2.0
output application/json
---
["AAPL", "MSFT", "NVDA", "TSLA", "CRM", "GOOG", "GOOGL", "V", "COST"] filter ($endsWith("A"))
This example passes into the filter function an array (before the keyword filter) and a lambda expression after the keyword filter. It returns a new array with values (ticker symbols) that end with “A”.
Note: Use the $$ syntax to filter based on index.
Example 4: Filter an array of objects
%dw 2.0
output application/json
---
payload
filter
(value, index) -> ((value.cik_str mod 2) == 1) and
(index > 5) and
(value.ticker startsWith ("T"))
This example passes into the filter function an array (before the keyword filter) and a lambda expression after the keyword filter. It returns a new array with values (objects) with cik_str keys that have odd values, values that are placed on odd indexes, and ticker keys that have values start with “T”.
Example 5: Filter array using the filter expression selector
%dw 2.0
output application/json
---
["AAPL", "MSFT", "NVDA", "TSLA", "CRM", "GOOG", "GOOGL", "V", "COST"] [?( $ contains "GO" )]
This example produces the same result as example 1. It passes into the filter expression an array and a lambda expression after the keyword filter. It returns a new array with values that contain the string “GO” from the unfiltered array.
Watch Tutorial
Follow along with the starter project.
I love that you provide the examples directly on the Playground!