Skip to content

Instantly share code, notes, and snippets.

@carlessanagustin
Created March 20, 2019 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carlessanagustin/2a29b35c5a6a4016a0a83b47f015d527 to your computer and use it in GitHub Desktop.
Save carlessanagustin/2a29b35c5a6a4016a0a83b47f015d527 to your computer and use it in GitHub Desktop.
JQ handy helpers

JQ handy helpers

(from https://github.com/vouill/jq-cheatsheet)

Testing them

find json online:

This repo > click on a JSON > view Raw and curl the url. ex:

curl https://raw.githubusercontent.com/LearnWebCode/json-example/master/pets-data.json

More complex ones:

Contentful example apps:

curl 'https://cdn.contentful.com/spaces/wl1z0pal05vy/entries?content_type=2PqfXUJwE8qSYKuM0U6w8M&include=3' -H 'Accept: application/json, text/plain, */*' -H 'Referer: http://localhost:8080/equipe' -H 'Origin: http://localhost:8080' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36' -H 'Authorization: Bearer 0e3ec801b5af550c8a1257e8623b1c77ac9b3d8fcfc1b2b7494e3cb77878f92a'

Pipe it

curl https://raw.githubusercontent.com/LearnWebCode/json-example/master/pets-data.json | jq

Cheat sheet

Using

 curl https://raw.githubusercontent.com/LearnWebCode/json-example/master/pets-data.json

Get specific value in the json

jq '.pets[2].name'

get values using array iterator in the json

jq '.pets[].name'

select only following a specific condition

jq '.pets[] | select(.species=="Dog")'

Restructure json

jq '.pets[] | {title: .name, photoUrl: .photo}'

List all attributes

jq '[path(..)|map(if type=="number" then "[]" else tostring end)|join(".")|split(".[]")|join("[]")]|unique|map("."+.)|.[]'

Ressources:

Doc

Cookbook

Trying it live

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment