Reputation: 9
I have the following input data
[
{
"key": "a",
"value": [
"1"
]
},
{
"key": "a",
"value": [
"2"
]
},
{
"key": "b",
"value": [
"3"
]
}
]
and would like to merge the values into an array, grouped by the same key. i.e. the following desired output:
[
{
"key": "a",
"value": [
"1",
"2"
]
},
{
"key": "b",
"value": [
"3"
]
}
]
any help is appreciated thank you!
Upvotes: 0
Views: 2354
Reputation: 4303
Similar approach. a bit different usage of functions.
%dw 2.0
output application/json
---
payload groupBy ($."key") mapObject {
"key": $[0].key,
"value": $ map {
temp: $.value[0]
}.temp
}
Upvotes: 2
Reputation: 1383
One option can be using groupBy
and then accommodating the groups you get like this:
valuesOf(payload groupBy ((item, index) -> item.key))
map ((group, index) -> {
"key": group[0].key,
"value": flatten(group.value)
})
This is producing the output you shared.
Upvotes: 3