Raw API output is usually one long line of braces. Formatting makes it readable, validating tells you whether it’s correct, and minifying shrinks it for the wire. They are three sides of the same job, and each one has a few details worth knowing.
Format (beautify)
Formatting, also called pretty-printing, adds line breaks and indentation so the structure is visible.
Before:
{"id":17,"name":"Ada","roles":["admin","editor"],"address":{"city":"Leeds","postcode":"LS1 1AA"}}
After:
{
"id": 17,
"name": "Ada",
"roles": [
"admin",
"editor"
],
"address": {
"city": "Leeds",
"postcode": "LS1 1AA"
}
}
To do this, paste the text into the JSON Formatter & Validator and choose an indent: two spaces, four spaces or tabs. Match your project’s convention; two spaces is the most common in JavaScript projects.
Formatting never changes the data, only whitespace between tokens, so a formatted file and its original parse to identical values.
Validate
Validation checks that the text is legal JSON. The formatter does this while you type and reports the exact line and column of the first error, which is far more helpful than the bare “Unexpected token” most parsers give.
Valid JSON, per the specification (RFC 8259), requires:
- Keys and strings in double quotes.
- No trailing commas.
- No comments.
- Values limited to object, array, string, number,
true,falseandnull. - Numbers without leading zeros, hex notation or
NaN/Infinity.
Our list of common JSON errors and fixes explains each of these in detail.
Note what “valid” does not mean. It means well-formed, not correct. {"age": "banana"} is valid JSON. To check that data has the right fields and types, validate against a schema; you can generate a JSON Schema from a sample.
Repair
Developers often have almost-JSON: a JavaScript object literal with single quotes and unquoted keys, a Python repr, or a config with comments. A repair option can fix the unambiguous problems, converting quotes, removing trailing commas and stripping comments. Review the result: automated repair guesses, and it cannot know what you meant by a half-missing record.
Minify
Minifying removes all unnecessary whitespace:
{"id":17,"name":"Ada","roles":["admin","editor"]}
Why bother?
- Smaller payloads. For deeply indented data, whitespace is often 10 to 30 percent of the file.
- One line per document, which suits log files and JSON Lines.
- Embedding. Putting JSON inside a shell command, an environment variable or a code string is easier when it is one line.
If your server uses gzip or Brotli, which it should, the difference in transfer size shrinks, because compression handles repeated whitespace well. Minify for storage and embedding; rely on compression for HTTP.
Sort keys
Sorting keys alphabetically gives a canonical form: two objects with the same content now produce the same text.
This is valuable for:
- Diffs. Comparing two files line by line is far cleaner when keys are in the same order. See how to compare two JSON files.
- Version control. Config files with sorted keys produce smaller commits and fewer merge conflicts.
- Hashing and caching. Identical content gives an identical string, and so an identical hash.
Sorting affects objects only; array order is meaningful and left alone. Be aware that JSON objects are officially unordered, but some programs depend on the order. If yours does, don’t sort.
Tips for readable JSON
- Use consistent indentation across the whole project, and enforce it with a formatter in your editor or pre-commit hook.
- Keep keys in a predictable order, with
idfirst. - Prefer short, stable key names. In a payload sent millions of times, they add up.
- Don’t format what a machine reads. Production APIs should send minified or compressed JSON.
- End files with a newline. It keeps tools such as
gitandcathappy.
Beyond the browser
The same operations on the command line:
jq . data.json # pretty-print
jq -c . data.json # minify
jq -S . data.json # sort keys
python -m json.tool data.json # validate and pretty-print
In JavaScript, JSON.stringify(value, null, 2) pretty-prints with two spaces, and JSON.stringify(value) minifies.
Big files
For files of many megabytes, pretty-printing multiplies the line count and can overwhelm editors. If you only need to look at records, a table is a better view; see how to view large JSON files.
Privacy
Everything above happens in your browser. Formatting and validation are done in JavaScript on your device, so config files and API responses containing secrets never leave it.
Next steps
Once your JSON is clean, query it with JSONPath, convert it to a table or turn it into TypeScript types.