filterObject function in DataWeave 2.0 (mulesoft)

The filterObjectoperator iterates over a list of key-value pairs in an object and applies an expression that returns only matching objects, filtering out the rest from the output. The expression must return true or false. If the expression returns true for a key, value, or index of an object, the object gets captured in the output. If it returns false for any of them, the object gets filtered out of the output. If there are no matches, the output array will be empty.

Syntax:

filterObject<K, V>(@StreamCapable value: { (K)?: V }, criteria: (value: V, key: K, index: Number) -> Boolean): { (K)?: V }
filterObject(Object<K,V>, ((V,K,Number) -> Boolean)): Object<K,V>

filterObject takes in an Object and a lambda that returns a Boolean. It then returns an Object with the same types as the input Object.

Input

{
  "name": "Jerry Schumann",
  "age": 34,
  "address": "123 Main Street"
}

DW Script

%dw 2.0
output json
---
payload filterObject (value, key, index) -> key == "age"

Output

{}

What happened? Well, we’ve hinted at this before.

All Object keys in DataWeave are of type Key, regardless of how the Object keys are created. The == operator tests if two values are equal, and part of that means checking that two values are the same type. This is why key == "age" returned false for every key-value pair in the input Object, Key == String is always false. How do you deal with this? There are three ways, you can:

  1. Cast the Key to a String with key as String == "age".
  2. Cast the String to a Key with key == "age" as Key.
  3. Use the “similar to” operator (~=), instead of the “equal to” operator (==), as key ~= "age"

The ~= operator and filterObject function usually go together. If you’re using filterObject to filter an Object based on a key, make sure you keep the ~= operator in mind!

Input

{
  "name": "Jerry Schumann",
  "age": 34,
  "address": "123 Main Street"
}

DW Script

%dw 2.0
output json
---
payload filterObject (value, key, index) -> key~="age"

Output

{
  "age": 34
}

Source

%dw 2.0
output application/json
var myObject = {
    str1 : "String 1",
    str2 : "String 2",
    str3 : null,
    str4 : "String 4",
}
---
myObject filterObject $ != null

Output

{
  "str1": "String 1",
  "str2": "String 2",
  "str4": "String 4"
}

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

Instagram

Facebook

Recent Posts

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

2 days ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago