jq - How to print key (not value of key) and iterate over keys to print sub value

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.


Here is a solution that uses keys directly:

  .indices
| keys[] as $k
| "\($k): \(.[$k].primaries.docs.count)"

Tags:

Jq