Here's my input data:
$array := [
{
"thing": "one",
"stuff": {
"type": "car",
"engine": "vrooom"
}
},
{
"thing": "two",
"stuff": {
"type": "truck",
"engine": "vrooom"
}
},
{
"thing": "three",
"stuff": {
"type": "car",
"engine": "vroom"
}
}
];
Ultimately I want the output to look like this:
{
"car": [
{
"thing": "one",
"stuff": {
"type": "car",
"engine": "vroom"
}
},
{
"thing": "three",
"stuff": {
"type": "car",
"engine": "vroom"
}
}
],
"truck": [
{
"thing": "two",
"stuff": {
"type": "truck",
"engine": "vrooom"
}
},
]
}
I've tried like this:
$reduce($array, function($accumulator, $value, $index){
$merge([$accumulator, {$value.stuff.type: [$accumulator.$value, $value]}])
}, {} )
but the output looks like this:
{
"car": [
{
"thing": "three",
"stuff": {
"type": "car",
"engine": "vroom"
}
},
{
"thing": "three",
"stuff": {
"type": "car",
"engine": "vroom"
}
}
],
"truck": [
{
"thing": "two",
"stuff": {
"type": "truck",
"engine": "vrooom"
}
},
{
"thing": "two",
"stuff": {
"type": "truck",
"engine": "vrooom"
}
}
]
}
I feel like it has something to do with $accumulator.$value
but I'm not sure how to get the syntax proper.
Here's a playground to try: https://try.jsonata.org/gM7o-EOV4