jq: Getting two levels of keys

Use map_values instead of map to map the values of a JSON object while preserving the keys: jq ‘.p | map_values(keys)’ On jq versions lower than 1.5, map_values is not defined: instead, you can use []|=: jq ‘.p | . []|= keys’

How do I sum all numbers from output of jq

the simplest solution is the add filter: jq ‘[.duration] | add’ the [ brackets ] are needed around the value to sum because add sums the values of an array, not a stream. (for stream summation, you would need a more sophisticated solution, e.g. using reduce, as detailed in other answers.) depending on the exact … Read more

jq combine output on a single line separated by space

The usual way would be to use the @csv or @tsv operators to convert the result in the CSV or tab-delimited format. These operators need the result to be contained in an array. For your case also to have a single space delimit, we can do a simple join(” “) operation jq -r ‘[.Accounts[].Id]|join(” “)’

jq: error (at :0): Cannot iterate over null (null)

To protect against the possibility that .items is not an array, you could write: .items | .[]? or even more robustly: try .items[] which is equivalent to (.items[])?. In summary: try E is equivalent to try E catch empty try E is equivalent to (E)? (Note that the expressions .items[]? and (.items[])? are not identical.) … Read more

Deleting multiple keys at once with jq

You can provide a stream of paths to delete: $ cat test.json | jq ‘map(del(.Country, .number, .Language))’ Also, consider that, instead of blacklisting specific keys, you might prefer to whitelist the ones you do want: $ cat test.json | jq ‘map({label, region, locale, currency})’