How to Format and Validate JSON
Make unreadable JSON readable, find the exact line of a syntax error, and fix the mistakes that break it most often.
4 min read
JSON is everywhere: API responses, config files, exports from apps, log lines. Machines usually write it as one long line with no spaces, which is efficient but impossible to read. And because JSON is strict, a single missing comma or an extra one makes the whole thing invalid. Formatting fixes the first problem, validating fixes the second.
What formatting and validating mean
- Formatting (prettifying) adds line breaks and indentation so that every key sits on its own line and nesting is visible at a glance.
- Minifying does the opposite: it removes all unneeded whitespace to make the data as small as possible, for example before pasting it into a config value.
- Validating checks that the text follows the JSON rules. A formatter has to parse the data first, so it validates as a side effect.
How to format JSON with QodeCat
The JSON Formatter & Validator runs entirely in your browser, so you can paste API responses that contain tokens or customer data without sending them anywhere.
- Paste your JSON into Raw Input, or click Upload to open a .json file. Uploaded files are formatted right away.
- Click Prettify to indent the data with two spaces, or Minify to put it on one line. The result replaces your input and appears with syntax highlighting on the right, marked Valid.
- The toolbar shows the size of the data and how many keys it has at the top level.
- Use the copy and download buttons to take the result with you. The download is saved as formatted_data.json.
If the JSON is broken, an Invalid JSON box appears with the error message from your browser, and your input stays as it was so you can fix it.
Reading the error message
A message such as Expected double-quoted property name in JSON at position 12 (line 3 column 1) tells you where the parser gave up, not always where the mistake is. In this case the real problem is a trailing comma at the end of line 2: the parser only notices when it reaches the closing brace. Look at the reported spot and at the character just before it.
The mistakes that break JSON most often
- Trailing commas: {"a": 1,} or [1, 2,]. JavaScript allows them, JSON does not.
- Single quotes: {'name': 'Ada'}. Keys and strings must use double quotes.
- Unquoted keys: {name: "Ada"}. Every key must be a string in double quotes.
- Missing commas between items, often after copying lines from different places.
- Comments (// or /* */). Standard JSON has no comments; remove them or use a format like JSONC where your tool supports it.
- Values that only exist in JavaScript: undefined, NaN, Infinity, or functions.
- Curly quotes (“ ”) pasted from a word processor or chat app instead of straight quotes.
Three things to know before you reformat
- Duplicate keys are silently merged. If an object contains the same key twice, only the last value survives formatting.
- Very large integers lose precision. Numbers above 9,007,199,254,740,991, such as some 64-bit IDs, are rounded when parsed, so 12345678901234567890 becomes 12345678901234567000. Keep such IDs as strings.
- Keys that look like whole numbers move to the front. In {"b": 1, "2": 0} the key "2" comes first after formatting. Other keys keep their order.
Working with related formats? Convert spreadsheets with CSV to JSON, switch config formats with YAML ↔ JSON, or generate types from a sample with JSON to TypeScript.
Frequently asked questions
Does formatting change my data?
Normally only the whitespace changes. Keys and values stay the same, with the three exceptions above: duplicate keys, integers too large for JavaScript and number-like keys moving to the front.
Can I format JSON with comments in it?
Not directly. Comments are not valid JSON, so the validator will report an error. Delete them first, then format.