How to Format and Validate JSON (and Find the Actual Error)
Invalid JSON errors are famously unhelpful when you are staring at one line of escaped text. Formatting helps only after the document can be parsed, so the first job is to find the exact character that broke the grammar.
The common failures
- A trailing comma after the last property or array item.
- Single quotes instead of JSON's required double quotes.
- An unescaped double quote or newline inside a string.
- JavaScript values such as
undefined,NaNor comments. - A missing closing brace or bracket far from where the parser finally gives up.
Paste the payload into the JSON formatter and validator. It reports the parser error with a line and column when the browser exposes a character position. Fix the first reported error and run it again; later errors are often just consequences of that one character.
Format for humans, minify for transport
Two-space indentation is a compact, readable default. Four spaces or tabs may match a project convention. Minifying removes only insignificant whitespace — it does not shorten strings, rename keys or change values. Keep readable JSON in reviewable files; minify delivery payloads only where bytes matter.
Sort keys carefully
Sorting object keys makes large configurations easier to compare and produces stable diffs. Array order is different: it can carry meaning and must not be rearranged. The tool sorts objects recursively while preserving every array position.
Do not paste secrets into random formatters
API responses can contain tokens, customer records and internal URLs. A server-backed formatter receives that material even if it promises deletion. Browser-local parsing avoids the transfer altogether. For tabular payloads that need Excel, continue with the JSON to CSV converter.