I have a JSON array like this:
[
{
"name": "Bo",
"type": "dog"
},
{
"name": "Roxer",
"type": "dog"
},
{
"name": "Paws",
"type": "cat"
}
]
I'm trying to convert it to an object keyed by type
, like this:
{
"dog": [
{
"name": "Bo",
"type": "dog"
},
{
"name": "Roxer",
"type": "dog"
}
],
"cat": [
{
"name": "Paws",
"type": "cat"
}
]
}
I found this answer which uses map, but it assumes the type
is unique and doesn't convert it to an array:
reduce .[] as $i ({}; .[$i.type] = $i)
The first time a value is inserted, it should be inserted as [$i]
. Any other time, it should append [] + $i
.