My fav jq commands

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the unix category.

Last Updated: 2022-05-26

$ jq is an instant classic unix command and likely here to stay. Some snippets:

Whole file pretty print

$ jq . my.json

Get sub-key called 'data'

$ jq .data file.json

or with optional (in this case) quotes

$ jq ".data" file.json

Get multiple sub-keys

$ jq ".data,.name" file.json

Get deeply nested data

$ jq .data.product_id.value file.json

or with piping (i.e. piping and dots can substitute for one another)

$ jq ".data | .product_id | .value" file.json

Key .name value for each item in a (top-level here) array

$ jq ".[] | .name" file.json

(outputs an array of names)

Merge entries from two keys

 jq '.devDependencies + .dependencies' package.json

Merge entries from multiple files

Get count of entities

# Just pipe to length
$ jq '.dependencies, .devDependencies | length' package.json

Filter records that have null at a value

$ jq '.[] | select (.coreTransactionData.productId == null)' file.json

Concat bits together to form one string

jq '.[] | "\(.name) -> \(.qr_code)"'

List the keys without values

jq '.properties | keys' file.json

Note that 'keys' has no dot in front of it (since it is a built-in command rather than a property of this particular piece of JSON)

Warp the output of a jq filter in an array (i.e. to appear as an array of objects and be valid for import)

Transform a command like so:

jq '.[] | select ((.payload.coreTransactionData.productId != null))' paymentConfirmedEvents--FromDeals.json

into this: (i.e. wrap the entire command in the array notation, and the do the piping within this array.)

$ jq '[.[] | select ((.payload.coreTransactionData.productId != null))]' paymentConfirmedEvents--FromDeals.json

How to handle non standard key names that might conflict with built-ins

Imagine the data had keys "schema:name" (i.e. with colons in them)

# This fails
json | jq '.schema:name'

However, wrapping in square brackets and strings works:

json | jq '.["schema:name"]'

How to handle when a key sometimes has an array and sometimes a single object

Use max to get last item in array. If it fails, ignore error with ?. Use the alternative operator // to fall back to . which (I THINK) is a no-op here.

cat ../transparenzportal-hamburg/freeformatter-out.json| jq '.events.Event[] | .eventDates.EventDate | (max? // .) .date'

How to remove double quotes are entries

Say if you want "jack" to just be jack. Do this (-r stands for raw-output)

jq -r '.name'