Skip to main content

TIL: jq Recipes for Everyday JSON Wrangling

1 min read

1. Extract a field from an array

curl -s https://api.example.com/users | jq '.[].name'

2. Filter and reshape

cat data.json | jq '.[] | select(.status == "active") | {id, email}'

3. Build a CSV header + rows

jq -r '["name","email"], (.[] | [.name, .email]) | @csv' data.json > output.csv

4. Group by a field

jq 'group_by(.status) | map({status: .[0].status, count: length})' data.json

5. Pretty-print with nested structure

jq '{total: length, items: .}' data.json

What I learned

jq is one of those tools where investing 30 minutes in the manual pays back every week. The select, map, and group_by functions cover 90% of what I need.