← All cheatsheets

Data

JSON wrangling with jq

## Basics
`jq . file.json` — pretty-print
`jq '.field' file.json` — extract field
`jq '.array[]' file.json` — iterate array
`jq '.array | length'` — array length

## Filter + transform
`jq '.[] | select(.status=="active")'` — filter
`jq '.[] | {id, name}'` — pick fields
`jq -r '.[].email'` — raw output (no quotes)
`jq '.[] | .name + " <" + .email + ">"' -r` — string concat

## With curl
`curl -s api/users | jq '.[].name'`
`curl -s api | jq '. | length'`
`jq '. + {new_field: "value"}' file.json` — add field
`jq '.field |= ascii_downcase' file.json` — transform field