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"