Basics

Pretty-Print & Minify JSON with jq (. and -c)

2 min read

The simplest jq use: clean up JSON. The identity filter . reads and re-emits the input, pretty-printed.

Pretty-print a file

jq '.' data.json

Pretty-print a curl / API response

curl -s https://api.example.com/thing | jq '.'

Minify to one line

-c (compact) puts each value on a single line — great for logs or re-piping:

jq -c '.' data.json

Input:

{ "name": "Ada",
  "tags": ["dev", "math"] }

Output:

{"name":"Ada","tags":["dev","math"]}

Sort object keys

jq -S '.' data.json

-S (sort-keys) emits every object’s keys alphabetically — handy for stable diffs.

Edit a file in place (jq has no -i)

jq can’t edit in place; write to a temp file and move it:

jq '.' data.json > data.tmp && mv data.tmp data.json

Tip: to validate JSON, just run jq . file.json — if it’s malformed, jq prints a parse error with the line/column. Use jq -e . to also get a non-zero exit code on null/false for scripting.

Open the full version (with copy buttons) ↗

← All recipes