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: 2024-11-21

$ 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 multiple sub-keys in an object along with values

jq '{program,message}' - note lack of dot

How to "de-array" something?

Place [] at the end of the key

jq .results[]

Get nth item of an array off a sub-key

# 0th item off the .results key
jq '.results | .[0]'

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)

make sure you don't forget to include any intermediate object keys

e.g.

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

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
16
15

Filter records that have null at a value

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

Concat bits together to form one string

Use parenteses and the plus operator

 jq '(.program + ":::" + .message)'

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'