jq slice and reshape JSON

jq reads JSON and applies a filter to it: pick fields, walk into nested arrays, select matching items and build new objects. It pairs well with sqx: jq reshapes nested JSON into flat records, and sqx joins, groups and sorts them.

Availability Install with brew install jq, apt install jq, dnf install jq or winget install jqlang.jq

Syntax

jq [options] 'filter' [file ...]

Common options

OptionWhat it does
.Pretty-print the input unchanged
.field / .a.bGet a field, or a nested field
.[]Every element of an array
select(cond)Keep items where the condition is true
-rPrint strings without quotes
-cCompact output, one JSON value per line (JSON Lines)

Examples

Pretty-print JSON

jq . response.json

List every name

jq -r '.[].name' users.json

Turn an array into JSON Lines

jq -c '.[]' users.json > users.jsonl

Only failed requests

jq -c 'select(.status >= 400)' requests.jsonl

Use jq with sqx

Unwrap nested records, then aggregate

Many APIs put the records inside a key such as items. jq pulls them out as flat JSON Lines; sqx does the grouping.

curl -s 'https://api.github.com/search/repositories?q=sqlite+cli' \
  | jq -c '.items[] | {name: .full_name, stars: .stargazers_count, language}' \
  | sqx "SELECT language, COUNT(*) AS repos, SUM(stars) AS stars FROM stdin GROUP BY language ORDER BY stars DESC"

Or read nested fields in SQL

For one or two nested values, SQLite's json_extract is often enough.

sqx -f events=events.jsonl "SELECT id, json_extract(meta, '$.device') AS device FROM events"

Full documentation

This page covers the essentials. For every option, see the official documentation:

Or read the manual in your terminal: man jq