top of page

How to filter objects using DataWeave

Updated: May 28, 2023

Filtering out unwanted values is a must when writing data transformations, and DataWeave can easily do this. This post shows you how to filter values in an object using DataWeave 2.0’s filterObject function and filter selector. If you want to filter values in an array, please check my post for filtering arrays using DataWeave.





Quick Navigation:

Anatomy of an object

An object is a data structure in DataWeave that is made up of a key-value pair. You can create an object in DataWeave by using the following syntax:


K:V or {K:V} 

K represents the “Key,” and V represents the “Value.”


$ syntax in objects

You can use the $, $$, $$$ symbols when building lambda expressions for functions that require an object.

Symbol

Description

$

​Represents the current value of the object.

$$

​Represents the current key of the object.

$$$

​Represents the current index of the object.

What is DataWeave’s filterObject Function?

filterObject() is a function in the dw::core library that takes an object and filters out values NOT met by the condition. The function will return a new object with values met by the filter condition.


You may look at this function and say, why would I need to filter an object? It’s just a key-value pair. However, this function is great for complex objects where some objects may be multiple-levels nested.



Infix Notation: Long-Hand


{ } filterObject (value,  key, index) -> (condition)  

Infix Notation: $ syntax


{ } filterObject (condition with $, $$ , $$$ syntax) 

Prefix Notation


filterOject({ }, (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 Selector?

DataWeave also provides the filter selector as a way to filter object values. Unlike the filterObject function, this expression also works on arrays.


Filter Expression


{ } [? (<boolean_expression>) ]

Explore filterObject Examples


Example 1: Filter object by value


%dw 2.0
output application/json
---
payload
filterObject
(value, key, index) -> ((value.ticker contains "GO"))

This example passes into the filterObject function an object (before the keyword filterObject) and a lambda expression after the keyword filterObject. It returns a new object with values where the ticker string “GO” from the unfiltered object.



Example 2: Filter object by key


%dw 2.0
output application/json
---
payload
filterObject
(value, key, index) -> (((key as Number) mod 2) == 0)

This example returns a new object with even keys from the unfiltered object.


Example 3: Filter object by index


%dw 2.0
output application/json
---
payload
filterObject
(value, key, index) -> (((index) mod 2) == 0)

This example returns a new object with values that had an even index from the unfiltered object.



Example 4: Filter object by value with $ syntax


%dw 2.0
output application/json
---
payload
filterObject
$ is Object

Returns a new unfiltered object.



%dw 2.0
output application/json
---
payload
filterO
bject
$ is Array

Returns an empty object, since none of the values in the object are arrays.




Example 5: Filter object by key with $$ syntax


%dw 2.0
output application/json
---
payload
filterObject
((($$ as Number) mod 2) == 0)

This example returns a new object with even keys from the unfiltered object.



Example 6: Filter object by index with $$$ syntax


%dw 2.0
output application/json
---
payload
filterObject
(($$$ mod 2) == 0)


This example returns a new object with values that had an even index from the unfiltered object.



Example 7: Filter object using the filter expression selector


%dw 2.0
output application/json

---
payload [?( $.ticker contains "GO" )]

This example passes into the filter expression selector an object and a lambda expression. It returns a new object with values where the ticker string “GO” from the unfiltered object.



Watch Tutorial

Follow along with the starter project.

Recent Posts

See All
bottom of page