jq basics

Read, filter and reshape JSON in the terminal.

SNP 095 #4B2 Filed jq cmd

Pretty print:

shell
curl -s https://api.example.com/posts | jq .

Pick fields:

shell
jq '.[0].title' posts.json
jq '.[] | .title' posts.json
jq -r '.[] | "\(.id)\t\(.title)"' posts.json

-r prints raw strings without the quotes, which is what you want when piping further.

Filter and reshape:

shell
jq '[.[] | select(.draft == false) | {id, title}]' posts.json
jq 'map(select(.tags | index("webdev")))' posts.json
jq 'sort_by(.publishedAt) | reverse | .[0:5]' posts.json

No jq? python3 -m json.tool posts.json at least pretty prints it.