grep for JSON

cf

Installation

brew install jq

TODO: I need to document this

curl -s https://daringfireball.net/feeds/json | jq '.items[].title'

Obviously ./hockeyNameToIdMapping.json would usually be living outside the Bash script - a Gist perhaps?

If you want to put comments in JSON then use JSmin to remove them.

See JSON Feed for more interesting stuff.

Using with az

List name of all Azure subscriptions.

az account list | jq '.[].name'

Examples

Mainly taken from this Medium article & the official docs.

echo '{ "foo": 123, "bar": 456 }' | jq '.foo'
echo '{ "Version Number": "1.2.3" }' | jq '."Version Number"'
echo '[1,2,3]' | jq '.[]'
echo '[ {"id": 1}, {"id": 2} ]' | jq '.[].id'
echo '["foo", "bar"]' | jq '.[1]'
echo '["foo", "bar"]' | jq '.[]' | touch
echo '{ "a": 1, "b": 2 }' | jq 'keys | .[]' # Note the pipe (|) operator is 'inside' the query
echo '[1,2,3]' | jq 'length'
jq -r '[(.dependencies, .devDependencies) | keys] | flatten | length' package.json # That returns the number of dependencies and devDependencies a package.json contains

cat taskJQ.json | jq '.value[]  | select(.id | contains("3e7980fc-6688-4473-9277-f7d654f27dbe"))' |  jq '.version | select(.major == 0) | .patch'

Some Tutorials

  • https://zerokspot.com/weblog/2013/07/18/processing-json-with-jq/
  • https://stedolan.github.io/jq/tutorial/
  • https://robots.thoughtbot.com/jq-is-sed-for-json
  • https://github.com/stedolan/jq/wiki/Cookbook
  • http://blog.librato.com/posts/jq-json