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

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

1 day ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

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

1 month ago

Estimate π Using Monte Carlo Method

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

2 months ago

Longest Substring with K Distinct Characters

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

2 months ago