JSON fundamentals
How to format and validate JSON correctly
Learn a reliable workflow for making JSON readable while confirming that the document remains valid strict JSON.
Document summary
A practical guide to indentation, syntax validation, key order, parser errors, and safe output review.
Key takeaways
- Use strict, representative input before relying on output.
- Review structure, types, and edge cases instead of checking only visual formatting.
- Continue examples inside the connected Bacodev tool to verify the result.
Formatting and validation solve different problems
Formatting changes whitespace and indentation. Validation checks whether the structure follows strict JSON grammar.
A document can look readable and still be invalid because of a trailing comma, comment, single quote, or unescaped character.
Use a repeatable validation workflow
- 1
Keep a copy of the original input.
- 2
Parse the input before applying formatting.
- 3
Read the first error location and inspect the surrounding characters.
- 4
Format only after the parser accepts the document.
- 5
Compare important values before replacing the source file.
Remember what strict JSON permits
| Element | Valid JSON | Common mistake |
|---|---|---|
| Property names | Double quoted | Bare or single quoted names |
| Commas | Between values only | Trailing comma |
| Special values | true, false, null | undefined, NaN, Infinity |
| Comments | Not supported | // or block comments |
Compare invalid and valid structure
{
name: 'Report',
active: true,
}{
"name": "Report",
"active": true
}Check more than syntax before production
- Confirm required fields exist.
- Confirm numbers have not become strings.
- Review null values and empty arrays.
- Check payload size after minification.
- Validate against a schema when the response is a contract.
Understand the parser messages
When validation fails, the message tells you where to look. "Unexpected token" points at a character the parser did not expect, "Unexpected end of JSON input" means the document stops early, and "Trailing comma" names the exact rule that strict JSON violates.
Each message maps to a concrete fix. The JSON error reference explains the common messages with broken and corrected examples, and the JSON repair tool applies conservative fixes you can review before using the result.
Run this workflow in the JSON formatter
- 1
Paste or open the JSON document in the JSON formatter. Files up to 25 MB are read locally.
- 2
Choose an indentation and run the formatter. The document is re-parsed, so any syntax error is reported immediately.
- 3
Switch to the tree or table view to inspect structure and types before you trust the output.
- 4
For schema checks, pass the same document to the JSON schema validator with a schema file.
Frequently asked questions
Is formatting safe for my data?
Yes. Formatting only re-indents and re-orders keys; it never changes values. Validation runs a strict parse so the output is guaranteed to be valid JSON when the tool reports success.
What does "strict JSON" reject that my editor accepts?
JavaScript object literals are looser than JSON. Strict JSON rejects trailing commas, single-quoted keys and strings, comments, and unquoted keys.
Does formatting upload my document?
No. The formatter reads the file or pasted text in your browser and formats it locally. Nothing is uploaded or stored.
What is the difference between format and minify?
Formatting adds indentation and line breaks for reading; minifying removes all whitespace for the smallest payload. Both produce the same data.