Converting a list of objects to an array
Given I have a list of objects:
{
"alpha": {
"bytes": {
"value": 4789440
},
"doc_count": 7723
},
"beta": {
"bytes": {
"value": 4416862639
},
"doc_count": 1296
}
}
Is there a way using jq
to get an array representation of this, such as:
[
{
"key": "alpha",
"bytes": {
"value": 4789440
},
"doc_count": 7723
},
{
"key": "beta",
"bytes": {
"value": 4416862639
},
"doc_count": 1296
}
]
Using to_entries
would be one way. By accessing .key
and .value
, the composition would be:
jq '[to_entries[] | {key} + .value]' input.json
[
{
"key": "alpha",
"bytes": {
"value": 4789440
},
"doc_count": 7723
},
{
"key": "beta",
"bytes": {
"value": 4416862639
},
"doc_count": 1296
}
]
Demo
[ keys_unsorted[] as $key | { $key } + .[$key] ]
Demo on jqplay
or
to_entries | map( { key } + .value )
Demo on jqplay