| name | jq-cli |
| description | Use jq to parse, filter, transform, and format JSON data from the terminal. Essential for processing API responses and structured data. |
jq
Lightweight command-line JSON processor. Parse, filter, map, and transform JSON.
Basic Syntax
echo '{"name":"John"}' | jq '.'
echo '{"name":"John"}' | jq '.name'
echo '{"name":"John"}' | jq -r '.name'
cat data.json | jq '.items[0]'
cat data.json | jq '.items | length'
Common Patterns
Object Access
jq '.user.name'
jq '.user.name // "default"'
jq '{name: .user.name, id: .user.id}'
jq 'keys'
jq 'to_entries'
Array Operations
jq '.[0]'
jq '.[-1]'
jq '.[2:5]'
jq '.[] | .name'
jq '[.[] | .name]'
jq 'map(.name)'
jq 'map(select(.age > 30))'
jq 'sort_by(.name)'
jq 'group_by(.category)'
jq 'unique_by(.id)'
jq 'first'
jq '[limit(5; .[])]'
Filtering
jq '.[] | select(.status == "active")'
jq '.[] | select(.name | test("^A"))'
jq '.[] | select(.count > 10)'
jq '.[] | select(.tags | contains(["beta"]))'
Transformation
jq '.[] | {name, email}'
jq '.items | map({id, title: .name})'
jq '[.[] | . + {processed: true}]'
jq '.[] | del(.password)'
jq '.items | map(.price) | add'
jq '[.[] | .amount] | add / length'
String Operations
jq -r '.[] | "\(.name): \(.email)"'
jq '.name | ascii_downcase'
jq '.name | split(" ")'
jq '[.[] | .name] | join(", ")'
Input/Output
jq -r '.[] | [.name, .email] | @csv'
jq -r '.[] | @tsv'
jq -r '@html'
jq -r '@uri'
jq -s '.'
jq -c '.'
Agent Best Practices
- Use
-r (raw output) when the result is a string you want to use
- Use
-e (exit status) for conditional checks: jq -e '.error' && echo "has error"
- Use
-c for compact output when piping to other tools
- Use
-s (slurp) to combine multiple JSON inputs into one array
- Use
// for default values to handle null fields gracefully
- Chain filters with
| for complex transformations
- Use
@base64d to decode base64 values in JSON
Example Workflows
Parse API response
curl -s https://api.example.com/users | jq '[.data[] | {id, name, email}]'
Transform and filter
cat orders.json | jq '[.[] | select(.total > 100) | {id, customer: .user.name, total}] | sort_by(-.total)'