I can print the keys:
$ jq -r '.indices | keys[]'
plan
resource
user
I can print the sub value:
$ jq '.indices[] | .primaries.docs.count'
14208
1427143
104475
How do I print the key and subvalue?
plan: 14208
resource: 1427143
user: 104475
Here is a solution that uses keys directly:
.indices
| keys[] as $k
| "\($k): \(.[$k].primaries.docs.count)"
The following might work:
$ jq -r '.indices | to_entries[] | "\(.key): \(.value.primaries.docs.count)"' input.json
plan: 14208
resource: 1427143
user: 104475
The above assumes the input is:
{
"indices": {
"plan": { "primaries": { "docs": { "count": 123 }}},
"resource": { "primaries": { "docs": { "count": 456 }}},
"user": { "primaries": { "docs": { "count": 789 }}}
}
}
to_entries
will convert the object indices
to an array:
[
{ "key": "plan", "value": { ... } },
...,
...
]
Which can then be easily mapped.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With