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

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago

Binary Search Tree (BST)

A Binary Search Tree (BST) is a type of binary tree that satisfies the following…

1 year ago