Reputation: 29
I am trying to map the below json payload to only show dates with "999" value. Below is my payload
{
"Delivery Date": "16",
"17/01/2018": "0",
"18/01/2018": "999",
"19/01/2018": "999",
"20/01/2018": "0",
"29/01/2018": "999",
"18/02/2018": "0",
"19/02/2018": "999",
"20/02/2018": "999",
"03/03/2018": "999"
}
And my desired output is
{
"available_date": [
"18/01/2018",
"19/01/2018,
"29/01/2018",
"19/02/2018",
"20/02/2018",
"03/03/2018"
],
"message": ""
}
Thanks for any advice!
Upvotes: 1
Views: 116
Reputation: 25664
The other solutions are good. Just for completeness I'll contribute one using filterObject() then pluck().
%dw 2.0
output application/json
---
{
available_date: payload
filterObject ($ == "999")
pluck ((value, key, index) -> key),
message: ""
}
Upvotes: 2
Reputation: 2835
Alternative using filterObject
and keysOf
:
%dw 2.0
output application/json
---
{
'available_date' : keysOf(payload filterObject ((value, key, index) -> value contains "999")),
message: ""
}
Upvotes: 3
Reputation: 2431
I have tried this.
%dw 2.0
output application/json
---
{
"available_date":
(payload mapObject ((value, key, index) -> ((key): (value)) if (value == "999")) pluck $$),
"message":""
}
Approach 2 FilterObject, keysOf
%dw 2.0
output application/json
---
{
"available_date": keysOf(payload filterObject ((value, key) -> value == "999")),
"message":""
}
Output
{
"available_date": [
"18/01/2018",
"19/01/2018",
"29/01/2018",
"19/02/2018",
"20/02/2018",
"03/03/2018"
],
"message": ""
}
Upvotes: 2