An API response arrives as one enormous line. A config file someone edited by hand suddenly makes the application refuse to start. A colleague pastes a payload into a chat and asks why the server returns "400 Bad Request". In all three cases the first job is the same: turn the JSON into something a human can read, and find out whether it is actually valid.
This guide explains what "formatting" JSON really changes, walks through beautifying and minifying it in the browser, shows the equivalent one-liners for the command line and code, and then covers the syntax mistakes that cause almost every "invalid JSON" message you will ever see.
What formatting does (and does not) change
JSON is defined by a very small grammar (RFC 8259 and ECMA-404). Outside of strings, whitespace has no meaning at all. That is why the same data can be written in two completely different shapes:
{"invoice":"INV-1001","total":249.5,"paid":true,"items":[{"sku":"A1","qty":2}]}
{
"invoice": "INV-1001",
"total": 249.5,
"paid": true,
"items": [
{
"sku": "A1",
"qty": 2
}
]
}
Both documents parse to exactly the same object. Beautifying (pretty-printing) adds line breaks and indentation so nesting is visible. Minifying strips every optional space and newline so the payload is as small as possible. Neither operation changes a key, a value or the order of keys.
There is one subtle point: a formatter has to parse the JSON before it can reprint it. So a formatter is also a basic validator. If the text is not valid JSON, it cannot be formatted, and that failure is often the most useful thing the tool tells you.
Format or minify JSON in your browser
The JSON Formatter parses your text with the browser's own JSON engine and reprints it. Nothing is sent to a server, which matters when the payload contains customer records or access tokens.
- Open the JSON Formatter and paste your JSON into the Input JSON box.
- Click Format / Beautify to reprint it with two-space indentation, one key per line.
- Or click Minify to collapse it into a single compact line, ready to paste into a request body or an environment variable.
- Use the Copy button above the output to put the result on your clipboard.
If the input cannot be parsed, the tool shows an Invalid JSON message instead of an output, with the line and column where parsing stopped and the parser’s explanation. For extra statistics about a valid document, such as its depth and key count, use the JSON Validator. For very large documents that are hard to scan even when indented, the JSON Viewer lets you collapse and expand branches of the tree.
Other ways to format JSON
A browser tool is quickest for a one-off paste. When JSON is part of your daily work, these built-in options are worth knowing.
In JavaScript or Node.js
JSON.stringify takes an optional third argument for indentation:
const data = JSON.parse(rawText);
console.log(JSON.stringify(data, null, 2)); // pretty, 2 spaces
console.log(JSON.stringify(data)); // minified
In Python
Python ships a formatter as a module you can run directly:
python -m json.tool response.json
python -m json.tool --compact response.json
With jq
If you have the jq command-line tool installed, jq . file.json pretty-prints and colours the output, and jq -c . file.json minifies it. jq is also excellent for pulling out a single field from a large response.
In your editor
Most code editors can format JSON natively. In VS Code, open the file (or set the language mode to JSON) and run Format Document. This is the better choice when you want to keep editing the file afterwards rather than just read it.
The errors behind "invalid JSON"
JSON looks like JavaScript, and that is the source of most mistakes. Many things that JavaScript, YAML or Python happily accept are forbidden in JSON.
| Mistake | Invalid | Valid |
|---|---|---|
| Trailing comma | {"a": 1,} |
{"a": 1} |
| Single quotes | {'a': 'x'} |
{"a": "x"} |
| Unquoted key | {a: 1} |
{"a": 1} |
| Comment | {"a": 1 // note} |
remove the comment |
| Special numbers | NaN, Infinity, 0x1F |
null or a plain decimal |
| Leading zero | {"code": 007} |
{"code": "007"} |
| Python literals | True, None |
true, null |
Trailing commas
The single most common error. A list that has been edited by deleting the last line often leaves a comma behind: [1, 2, 3,]. JavaScript and many config formats accept this; strict JSON parsers do not.
Quotes and escaping
Strings and keys must use straight double quotes. Curly "smart" quotes copied from a word processor or a chat app look almost identical but are different characters, and they will break parsing. Inside a string, a literal double quote must be escaped as \", a backslash as \\, and a real line break must be written as \n. Windows paths are a frequent trap: "C:\new\data" contains \n and \d, which is either the wrong character or an invalid escape. Write "C:\\new\\data" instead.
Comments
Standard JSON has no comments. Files such as tsconfig.json or VS Code settings accept them because those tools use a relaxed parser, but the same file will fail in a strict one. Remove comments before sending JSON to an API.
Something that is not JSON at all
If the error points at the very first character, check what you actually pasted. Common culprits are an HTML error page returned instead of JSON, a log line prefix such as a timestamp, a byte order mark at the start of a file, or a JavaScript object copied from a console that uses unquoted keys.
Tips for working with large or sensitive JSON
- Minify only for transport. Keep formatted JSON in version control so that diffs show exactly which line changed. The JSON Compare tool is useful when two versions differ in ways a text diff makes hard to see.
- Watch big numbers. JavaScript stores numbers as 64-bit floating point, so integers larger than 9,007,199,254,740,991 lose precision when parsed in a browser. If IDs are that large, the API should send them as strings.
- Duplicate keys are silently merged.
{"a": 1, "a": 2}usually parses, and the last value wins. Formatting will quietly show only one key, so check the source if a value seems to be missing. - Keep secrets local. Access tokens, personal data and internal URLs should not be pasted into tools that upload text. A browser-only formatter, or your own editor, avoids that risk.
Frequently asked questions
Does formatting JSON change the data?
No. Beautifying and minifying only add or remove whitespace between tokens. Keys, values and key order stay the same, and whitespace inside strings is never touched.
Why does my JSON work in JavaScript but fail in a validator?
JavaScript object literals allow single quotes, unquoted keys, trailing commas and comments. JSON allows none of those, so text that is valid JavaScript can still be invalid JSON.
How many spaces should JSON be indented with?
There is no rule. Two spaces is the most common convention and is what our formatter uses; four spaces or tabs are equally valid. Pick one per project and let your tools apply it.
Can JSON contain comments?
Not in the standard. Some tools accept a relaxed variant (often called JSONC) with comments, but APIs and strict parsers will reject it. Store explanations in a separate field or in documentation instead.
Why does the formatter say my JSON is invalid when it looks fine?
Look for invisible problems: a trailing comma on the last item, curly quotes, a byte order mark, or text before the opening brace. A validator that reports the line and column, such as the JSON Validator, makes these much faster to find.