How do I pretty print JSON in the terminal with jq?

Dark navy cover card with the title 'How do I pretty print JSON in the terminal with jq?' in white and a green accent bar

To pretty print JSON with jq, run jq . file.json, or pipe output into it with curl -s https://api.example.com/users | jq .. The dot is jq's identity filter, so jq parses the JSON and prints the same data back with two-space indentation and colors. Add --indent 4, --tab or -S (sorted keys) to change the layout.

Install jq first

jq is a single command-line program, and the official download page lists a package for every major system. As of October 2026 the latest release is jq 1.8.2.

System Command
Debian, Ubuntu sudo apt-get install jq
Fedora sudo dnf install jq
macOS (Homebrew) brew install jq
Windows (winget) winget install jqlang.jq
Windows (Scoop / Chocolatey) scoop install jq or choco install jq

Check it worked with jq --version.

How to pretty print JSON with jq

Pretty printing JSON with jq takes one filter: the dot. The jq manual describes . as the filter that "takes its input and produces the same value as output."

Give jq a file, or pipe text into it:

# A file on disk
jq . config.json

# Output from another command
curl -s https://api.github.com/repos/jqlang/jq | jq .

# A string you paste
echo '{"name":"Ada","langs":["js","go"]}' | jq .

The last command prints:

{
  "name": "Ada",
  "langs": [
    "js",
    "go"
  ]
}

Skip cat file.json | jq .. Passing the file name does the same job with one less process.

jq also reads several JSON values in a row. The manual says input "is parsed as a sequence of whitespace-separated JSON values", so a log file with one JSON object per line comes out as a series of formatted objects.

Change indentation, sort keys and minify

jq's output flags change the layout without touching the data. The ones you'll use most, from the jq manual:

  • --indent n uses n spaces per level. The limit is 7.
  • --tab uses one tab per level instead of two spaces.
  • -S (--sort-keys) prints every object's keys in sorted order.
  • -c (--compact-output) puts each JSON object on a single line, which is how you minify with jq.
  • -r (--raw-output) prints a string result without its quotes.

Here's where -S earns its place. Two API responses with the same fields in a different order look different in a diff tool. Run both through jq -S . first and only real changes show:

jq -S . before.json > before.sorted.json
jq -S . after.json > after.sorted.json
diff before.sorted.json after.sorted.json

Save the formatted JSON without wiping the file

To save pretty printed JSON back to the same file, write to a temporary file first and then move it. Never run jq . data.json > data.json. The shell empties data.json for the redirect before jq reads it, and you lose the data.

The jq FAQ is direct about this: jq has no in-place option like sed -i, and using tee or output redirection to overwrite the input file "is not recommended, even if it seems to work." Its suggested pattern:

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

The && matters. If jq fails on bad input, mv never runs and the original survives. The FAQ also mentions sponge, which is part of GNU moreutils, if you prefer jq . data.json | sponge data.json.

Keep colors when you page through long output

jq colors its output only when it writes straight to a terminal. The manual says "by default, jq outputs colored JSON if writing to a terminal", so piping into less drops the colors.

Force them back on with -C, and tell less to show color codes with -R:

jq -C . big-response.json | less -R

The less man page describes -R as passing only a limited set of escape sequences through in raw form, which is what color codes are. To turn colors off instead, for example when writing to a log, use -M. You can change the color scheme with the JQ_COLORS environment variable.

Quote the filter correctly on Windows

On Windows, wrap the jq filter in double quotes, not single quotes. The jq manual says that with cmd.exe "it's best to use double quotes around your jq program", and that double quotes inside the program then need backslash escaping.

So jq '.name' users.json from a Linux tutorial becomes this in cmd.exe:

jq ".name" users.json

For plain pretty printing, jq . users.json needs no quotes at all, which is why I'd start there on any system.

Catch invalid JSON in scripts

jq doubles as a quick validator, because it refuses to print JSON it can't parse. Instead of output, you get a parse error on stderr, and the command exits with a non-zero status.

The manual lists the exit codes: 0 if the program ran, 2 for a usage problem or system error, and 3 for a jq program compile error. A minimal check:

if jq . payload.json > /dev/null; then
  echo "valid JSON"
else
  echo "payload.json is not valid JSON"
fi

For the common causes of a rejected file, see how to fix invalid JSON and what "Unexpected end of JSON input" means. jq only checks syntax. Required fields and types are a job for JSON Schema validation.

When jq isn't the right tool

jq is the right tool when the JSON is already in your terminal. For a file you have open in an editor, formatting it there is faster: see formatting JSON in VS Code.

My take: learn jq ., -S and the temp-file save pattern, and ignore filtering until a task forces it on you. Those three cover most days.

The limitation is the install. jq isn't always on locked-down servers or a colleague's Windows laptop, and you may not have rights to add it. There, pasting the text into a browser-based JSON formatter gets you the same indented view; the formatter on this site processes input in the page and its tool code makes no network requests.

FAQ

What does the dot mean in jq .?

The dot is jq's identity filter. It outputs its input unchanged, so jq parses the JSON and prints it back with indentation and colors.

How do I change the indentation in jq?

Use --indent n for n spaces, up to 7, or --tab for one tab per level. Without either, jq indents with two spaces.

Can jq edit a JSON file in place?

No. jq has no in-place option like sed -i. Write the result to a temporary file, then move it over the original.

Why does jq lose its colors when I pipe it into less?

jq only colors output when it writes to a terminal. Force colors with -C and let less show them with -R: jq -C . file.json | less -R.

How do I minify JSON with jq?

Run jq -c . file.json. The -c flag puts each JSON value on a single line.

Sources