Validate JSON with jq: What It Checks, What It Changes, and When to Use It
How to validate JSON with jq, what a passing check does and does not prove, how jq reformats data, and when the original text should be kept unchanged.
jq is a command-line JSON processor. Most people use it to filter or reshape data, but it is also a common way to check whether a file or a piped stream is valid JSON. This review covers what that check tells you, what jq does to your data when it prints it, and when you should keep the original text instead of jq's output. It is based on jq's documented behavior and general JSON principles, not on new test results. jq's behavior has changed between releases, so check your installed version with jq --version before relying on any edge case described here.
Is jq suitable for validating JSON?
For a quick syntax check, yes. jq parses its input as JSON and stops with an error message and a nonzero exit status when the input is not valid JSON. That makes it practical in a terminal, in shell scripts and in CI jobs where jq is already installed. The error message usually names the line and column where parsing failed, which helps when you are tracking down a missing comma or an unclosed bracket.
The key tradeoff is that jq checks syntax, not meaning. A passing check shows that jq could parse the input. It does not show that the data has the fields, types or values your application expects. jq also accepts some inputs that a stricter validator, or the program that will consume the file, may reject. The edge cases below explain which ones.
Keep the original text, not jq's output, when exact bytes matter. Examples include signed payloads, files compared by hash, fixtures that must match byte for byte, and documents where key order or number formatting is significant to a downstream system. In those cases use jq only to check the file, and discard its output.
Basic validation commands
The simplest check uses the empty filter. It parses the input and produces no output, so all you see is an error message, if there is one, and the exit status.
jq empty config.json
echo $?
In a script, branch on the exit status instead of parsing jq's messages:
if jq empty config.json 2>/dev/null; then
echo "valid JSON"
else
echo "invalid JSON"
fi
Running jq with the identity filter (a single dot) also validates, but it prints the parsed data. That is useful when you want to see the structure, and wasteful when you only need a yes or no.
jq . config.json
Edge cases a simple jq check can miss
- Multiple values in one input: jq reads a stream of JSON values, so input such as {"a":1}{"b":2}, or one object per line, parses successfully. If your consumer expects exactly one JSON document, this check alone will not catch the problem.
- Empty input: an empty file holds no JSON values, so jq empty has nothing to fail on and typically exits successfully. Many parsers reject an empty document.
- Duplicate keys: when an object repeats a key, jq keeps one value, the last one, without warning. The JSON specification does not require parsers to reject duplicates, but other tools may handle them differently, so the same file can mean different things in different programs.
- Large or high-precision numbers: older jq releases convert numbers to double-precision floating point, which can change very large integers or long decimals in the output. Newer releases preserve more of the original number text in some cases. If precision matters, test with your version and your data.
- Schema rules: jq has no built-in concept of required fields or types. You can write filters that check for them, but that is custom logic, not standard validation.
If a file must contain exactly one JSON value, you can check that explicitly. The -n flag tells jq not to read input automatically, inputs collects every value in the stream, and -e makes the exit status depend on whether the final result is true. A parse error still produces a nonzero exit.
jq -e -n '[inputs] | length == 1' config.json > /dev/null
For structural rules such as required keys, a filter used with -e can act as a lightweight assertion. The following exits nonzero unless the top-level value is an object containing a string field called name:
jq -e 'type == "object" and (.name | type) == "string"' config.json > /dev/null
Checks like this get hard to maintain as the rules grow. When you need to enforce many structural rules, a dedicated JSON Schema validator is usually the better choice.
What jq actually changes in the output
Validation with jq empty changes nothing, because it writes no data. Every other filter re-serializes the data: jq parses the input into values and then writes new JSON text. The source file is not modified unless you redirect the output over it. The printed text, however, is a fresh rendering, not a copy of the original.
- Whitespace and indentation are replaced by jq's own pretty-printing. With -c the output is compact, one value per line.
- Key order is preserved by default. With -S the keys are sorted, which changes the text but not the parsed data.
- String escapes may be normalized. For example, escaped characters in the input may come out as literal characters, or the reverse. The decoded string value stays the same.
- Number formatting may differ from the input, depending on the jq version, as described above.
- Duplicate keys collapse to a single entry.
Most of these changes keep the data the same for any consumer that parses JSON: the same objects, arrays, strings and booleans. They are not byte-for-byte identical, though, and the exceptions are duplicate keys and number precision. Those two can change what a consumer actually receives, which is why they are worth checking separately.
Will the output stay usable after copying or sharing?
jq's output is plain JSON text, so it can be pasted anywhere that accepts JSON. One common problem is color. jq can add terminal color codes to its output. By default it colors only when writing to a terminal, but -C forces color even when output is piped or redirected. Forced color inserts escape sequences that make a file invalid JSON and show up as stray characters when pasted elsewhere. Use -M, or leave out -C, when the output will be saved or shared.
Pretty-printed output is generally easier to read and to review in a diff than minified JSON, and sorting keys with -S can make two files easier to compare. Compact output is better for logs, for line-oriented tools and for places where size matters. Neither form affects how a JSON parser reads the data.
On accessibility, jq is a terminal program, so its usability with screen readers depends on the terminal and the assistive technology in use. No scoped test of specific screen readers or terminals was available for this review, so no claims are made about particular combinations. Some general points hold regardless. Color is a supplement, not a requirement: the error message and exit status carry the same information without it, and -M removes color entirely. Deeply nested, pretty-printed output can be long to navigate line by line, and in that situation a narrower filter that extracts only the relevant fields may be easier to work with.
Summary recommendation
- Use jq empty for a quick, scriptable check of whether input parses as JSON.
- Add an explicit check if the input must contain exactly one value or must not be empty.
- Use jq's re-serialized output for reading, reviewing and reshaping, but keep the original bytes where signatures, hashes or exact formatting matter.
- Test number precision and duplicate-key handling with your installed jq version if your data depends on them.
- Use a schema validator, not jq alone, when you need to enforce structure and types.
Gaps in this review
No version-specific test results, performance figures or comparisons with other validators were supplied, so none are given here. Version-dependent behavior, especially number handling and specific exit codes, should be confirmed against the manual for your installed release and a small test on your own data.