Usage of filterObject In Dataweave

 

In this tutorial we will demonstrate how can we use the filterObject in dataweave

Using filterObject we can Iterates 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.

Step 1

We will use the sample json object to demonstrate this, here we will fetch all the key,value where the value is true

Sample Json:

{
"isAdult": true,
"isEligibleForLoan":false,
"isIndian": true,
"isDefaulter":false
}

Dataweave Expression:

%dw 2.0
output application/json
---
payload filterObject ((value) -> value == true)

Output:

{
"isAdult": true,
"isIndian": true
}

Step 2

From the about output we get the JSON having all the key value pair for true values, now if we want to convert that to array of keys use pluck function

Input:

{
"isAdult": true,
"isIndian": true
}

Dataweave Expression:

%dw 2.0
output application/json
---
payload pluck $$

Output:

[
"isAdult",
"isIndian"
]

Step 3

Now if want to combine the key as a string use joinBy

Input:

[
"isAdult",
"isIndian"
]

Dataweave Expression:

%dw 2.0
output application/json
---
payload joinBy ","

Output

"isAdult,isIndian"

 

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments