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!
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": ""
}
Alternative using filterObject
and keysOf
:
%dw 2.0
output application/json
---
{
'available_date' : keysOf(payload filterObject ((value, key, index) -> value contains "999")),
message: ""
}
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: ""
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.