Parse JSON with jq

What jq changes when it parses and re-outputs JSON, when that's safe, and when you should keep the original text instead.

jq is a command-line JSON processor. It reads JSON text, parses it into values, runs a filter you supply, and writes the result back out as JSON or, if you ask, as raw text. That round trip is useful, and it can still alter your data. This review covers what jq changes, when the change is harmless, and when you should keep the original text.

Is jq the right tool for your use?

jq suits work where you care about the data rather than the exact characters. Its main benefit is that parsing doubles as validation: jq either produces well-formed output or stops with a parse error. Its filters let you extract fields, reshape objects, and pretty-print without editing by hand. The tradeoff is that jq re-serializes the data instead of passing your bytes through. Whitespace, escape sequences and duplicate keys can change, and number formatting can change too, depending on the version.

  • Keep the original text, or use jq only for reading and not rewriting, when:
  • Exact bytes matter, such as signed payloads, checksums, hashes, or files where formatting changes would add noise to diffs.
  • The input relies on duplicate keys, which jq collapses.
  • The input is not strict JSON, for example it has comments or trailing commas. jq rejects it rather than guessing.
  • The data holds very large integers or high-precision decimals, and you have not confirmed how your jq version handles them.

What jq actually changes

Two kinds of change can happen. Some are stored changes: the output contains different characters from the input. Others only affect how output looks in your terminal. The default filter, a single dot, outputs its input unchanged as data, but jq still rewrites the text.

$ echo '{"name":"Ana",   "tags":["a","b"]}' | jq .
{
  "name": "Ana",
  "tags": [
    "a",
    "b"
  ]
}

$ echo '{"name":"Ana",   "tags":["a","b"]}' | jq -c .
{"name":"Ana","tags":["a","b"]}

$ echo '{"name":"Ana"}' | jq '.name'
"Ana"

$ echo '{"name":"Ana"}' | jq -r '.name'
Ana
  • Stored changes to expect:
  • Whitespace and indentation are replaced. The default is two-space indentation, -c gives compact output, and --tab or --indent change the style.
  • Object key order is kept as written unless you pass -S, which sorts keys.
  • Duplicate keys collapse to a single key holding the last value.
  • Non-ASCII characters are written as UTF-8 by default. The -a option writes them as \u escape sequences instead. A JSON parser reads both forms as the same value, but they are different text.
  • The -r option strips JSON quoting from string results and unescapes them. That output is no longer JSON.
  • Numbers may be reformatted. Older jq releases converted every number to a double-precision float, so large integers could lose precision. Newer releases document that unmodified number literals are preserved. Check your version with jq --version and read the manual for that release before relying on either behavior.
$ echo '{"a":1,"a":2}' | jq -c .
{"a":2}

$ echo '{"city":"Zürich"}' | jq -c .
{"city":"Zürich"}

$ echo '{"city":"Zürich"}' | jq -ac .
{"city":"Z\u00fcrich"}

Color only affects presentation. jq colorizes output when it writes to a terminal and turns color off by default when output goes to a pipe or a file. If you force color with -C, ANSI escape codes become part of the output and appear as stray characters in files, tickets or chat messages. Use -M to turn color off explicitly.

What "reverse" means in jq

jq's reverse filter reverses the order of elements in an array. It changes the data. It does not mirror glyphs or switch text to right-to-left display, and jq has no font or styling features. Reversing a string is a different matter. The usual approach is explode, then reverse, then implode, and explode splits a string into Unicode code points, not visible characters.

$ echo '[1,2,3]' | jq -c 'reverse'
[3,2,1]

$ echo '"e\u0301a"' | jq -c 'explode'
[101,769,97]

$ echo '"e\u0301a"' | jq -c 'explode | reverse'
[97,769,101]

In the example, code point 769 is a combining acute accent. It belonged to the "e". After reversal it attaches to the "a", so the visible text changes meaning. Emoji built from several code points break apart in the same way, including sequences joined with zero-width joiners and those with skin-tone modifiers. jq's built-in string functions work on code points, not grapheme clusters, so code-point reversal is only safe for text you know has no such sequences.

Will the output stay usable after copying or sharing?

  • Valid JSON output parses to the same values in other standards-compliant parsers, whatever the whitespace. Reformatting is therefore safe for programs that parse JSON, subject to the number and duplicate-key caveats above.
  • Raw output from -r is plain text. Strings containing newlines or tabs come out as literal line breaks and tabs. If you paste that text where JSON is expected, it has to be quoted and escaped again.
  • Forced color codes corrupt pasted text. Leave color at its default when output will be saved or shared.
  • ASCII escaping with -a can help destinations that mishandle UTF-8, but people can no longer read the escaped characters. A search for the literal character will also miss the escaped form.
  • Reformatting changes line numbers and produces large diffs. Searching for a value still works, but searching for an exact line from the original formatting may fail.
  • Indentation shows nesting visually. Compact output is shorter but harder to scan. How screen readers announce brackets and deeply indented JSON was not tested for this review, so check with the assistive technology your readers actually use.

Readers who don't work with JSON usually need a plain-text version. Rather than sharing a JSON dump, use -r to pull out the values they need, written as simple lines or sentences.

Validating JSON with jq

$ jq empty file.json
$ echo $?

The empty filter prints nothing for valid input. For invalid input, jq prints a parse error and exits with a nonzero status, which makes the command easy to use in scripts. One caveat: jq accepts a stream of several JSON values in one input, such as two objects one after the other. Input that jq accepts is therefore not always a single JSON document. If the destination expects exactly one value, check that separately, for example with the -s option, which collects all input values into one array so you can count them.

Evidence gaps

This review draws on jq's general documented behavior and on simple command examples. It includes no version-specific benchmarks or measurements, and no tests against particular destinations, editors or screen readers. Number handling in particular differs between releases, so confirm it against the manual for your installed version. jq runs locally as a command-line program. Web-based jq playgrounds are separate services, and their data handling cannot be judged from how they look or how they describe themselves.