jq Tips 2019-10-21

jq is a fantastic command-line based tool to process JSON input. I use it more or less frequently, and just want to document some of the tools I need for further reference.

Note: jq can a LOT more than what I show here. Check the official docs if you want to know more.

Get only unique array entries, and output some of the content’s properties:

$ curl https://www.thurgaukultur-beta.ch/services/export/eventfrog | jq '.events | unique_by(.extId) | map({title: .title, id: .extId})' | less

Find certain array entries, and output only the unique ones (unique by a certain property):

curl https://www.thurgaukultur-beta.ch/services/export/eventfrog | jq '.events | map(select(.extId == "25198")) | unique_by(.extId)' | less

Count results:

curl https://www.thurgaukultur-beta.ch/services/export/eventfrog | jq '.events | unique_by(.extId) | {res: map({title: .title, id: .extId}), count: .|length}'

Update all package.json dev packages all at once to the newest ones:

jq -r '.devDependencies|keys|map(.+ "@latest")|.[]' package.json | xargs npm install -D