Skip to main content

jq

Command-line JSON processor.

Basics

  • Pretty-print
jq . data.json
  • Get a field
jq '.name' data.json
  • From stdin
curl -s https://api.example.com | jq '.items[0].id'

Selecting and mapping

  • Select by predicate
jq '.items[] | select(.active==true)' data.json
  • Map to smaller objects
jq '.items[] | {id, name, tags}' data.json
  • Extract unique values
jq '.items[].type | unique' data.json

Filtering arrays

  • Keep where size > 10
jq '[.items[] | select(.size>10)]' data.json
  • Sort by key
jq 'sort_by(.created_at) | reverse' data.json

Text output

  • Raw strings (no quotes)
jq -r '.items[].url' data.json
  • Join array into CSV-like lines
jq -r '.[] | [.id, .name] | @csv' data.json

Update/construct

  • Add/modify field
jq '. + {env: "prod"} | .count+=1' data.json
  • Build object from env var
jq --arg v "$VERSION" '{version: $v}'

Combine multiple files/streams

  • Slurp then operate
jq -s '[ .[] | .items[] ] | length' *.json

Tips

  • Use -c for compact output.
  • For large files, prefer streaming tools (jq -n --stream) but syntax differs.