JSON is the lingua franca of web development. Every API response, every config file, every NoSQL document — it's all JSON. Yet developers routinely waste time on formatting issues, validation errors, and subtle syntax traps. Here's how to handle JSON like a professional.
Why JSON Formatting Matters
Bad JSON formatting isn't just an aesthetic problem:
- Debugging: A 500-line minified JSON response is unreadable. Pretty-printing reveals structure instantly.
- Code review: Properly formatted JSON configs in version control show meaningful diffs.
- Performance: Minified JSON reduces payload size by 10-30%, which matters for mobile users and high-traffic APIs.
- Validation: Catching syntax errors before they hit production saves hours of debugging.
Format vs. Minify: When to Use Each
Pretty-Print (Format)
Use formatted JSON when humans need to read it:
{
"user": {
"id": "01ARZ3NDEKTSV4RRFFQ69G5FAV",
"name": "Alice",
"roles": ["admin", "editor"],
"settings": {
"theme": "dark",
"notifications": true
}
}
}
When to format:
- Debugging API responses
- Configuration files in version control
- Documentation examples
- Log inspection
- Database record inspection
Indent size: 2 spaces is the industry standard for JSON. It's what GitHub renders, what most linters default to, and what major open-source projects use. 4 spaces works too but wastes horizontal space for deeply nested structures.
Minify
Use minified JSON when machines consume it:
{"user":{"id":"01ARZ3NDEKTSV4RRFFQ69G5FAV","name":"Alice","roles":["admin","editor"],"settings":{"theme":"dark","notifications":true}}}
When to minify:
- API responses (reduce bandwidth)
- localStorage/sessionStorage (reduce storage)
- Log entries (one line per record for grep-ability)
- Message queues and event payloads
- Embedded JSON in URLs or query strings
Size impact: For a typical API response, minification reduces size by 15-25%. For deeply nested configs, savings can reach 30-40%.
The 7 Most Common JSON Errors
We analyzed thousands of JSON validation errors from our formatter tool. Here are the most frequent mistakes, ranked by occurrence:
1. Trailing Commas (37% of errors)
{
"name": "Alice",
"age": 30,
}
Why it happens: JavaScript and TypeScript allow trailing commas. JSON does not. Developers copy objects from code into JSON files without removing the last comma.
Fix: Most formatters strip trailing commas automatically. In VS Code, use "Format Document" with a JSON formatter extension.
2. Single Quotes (22% of errors)
{
'name': 'Alice'
}
Why it happens: Python developers use single quotes by default. YAML also uses single quotes. When converting to JSON, quotes get carried over.
Fix: Find-and-replace single quotes with double quotes. But be careful with strings containing apostrophes.
3. Unquoted Keys (15% of errors)
{
name: "Alice"
}
Why it happens: JavaScript object literals don't require quoted keys. JSON does.
Fix: Always wrap keys in double quotes.
4. Comments (11% of errors)
{
"debug": true,
"port": 3000
}
Why it happens: JSON does not support comments. Period. This is one of the most controversial design decisions in JSON's history. Douglas Crockford intentionally excluded them to prevent misuse as directives.
Fix: Use JSONC (JSON with Comments) for config files that need annotations. VS Code supports .jsonc natively. For standard JSON, remove all comments.
5. Missing Commas (8% of errors)
{
"first": "Alice"
"last": "Smith"
}
Why it happens: When adding new properties to existing JSON, developers forget the comma after the previous line.
Fix: Most JSON formatters will point to the exact position of the missing comma.
6. Undefined and NaN (4% of errors)
Why it happens: undefined and NaN are valid JavaScript values but not valid JSON. JSON.stringify() silently drops undefined properties and converts NaN to null.
Fix: Replace undefined with null. Replace NaN with null or a sentinel value like -1.
7. Hex Numbers and Special Literals (3% of errors)
Why it happens: JSON only supports decimal numbers and lowercase true/false/null. Hex literals and Python-style True/False are invalid.
Fix: Convert hex to decimal. Use lowercase booleans.
JSON in Different Contexts
API Development
For REST APIs, always:
- Return minified JSON in production (save bandwidth)
- Support a pretty query param for debugging
- Set
Content-Type: application/jsonheader - Use consistent key naming (camelCase or snake_case, not both)
Configuration Files
For config files (package.json, tsconfig.json):
- Use 2-space indentation
- Keep files sorted alphabetically where possible (reduces merge conflicts)
- Consider JSONC or JSON5 if you need comments
- Validate with schema (JSON Schema) for complex configs
Logging
For structured logging:
- One JSON object per line (NDJSON format)
- Always minify (one line = one log entry)
- Include timestamp, level, message as top-level fields
- Use consistent field names across services
Tools Comparison
<!-- comparison -->| Tool | Online | Offline | Validation | Minify | Syntax Highlight | Free |
|---|---|---|---|---|---|---|
| Temporary.Best | Yes | Yes (browser) | Yes | Yes | Line numbers | Yes |
| jq (CLI) | No | Yes | Yes | Yes | Terminal colors | Yes |
| VS Code | No | Yes | Yes | Yes | Full highlighting | Yes |
| jsonlint.com | Yes | No | Yes | No | No | Yes |
| JSON Editor Online | Yes | No | Yes | Yes | Tree view | Freemium |
Performance Tips
Large JSON files (>1MB):
- Use streaming parsers instead of
JSON.parse() - Avoid pretty-printing in production logs
- Consider binary formats (MessagePack, Protocol Buffers) for internal services
Browser performance:
JSON.parse()is faster thaneval()for parsing (and safer)JSON.stringify()with a replacer function lets you filter sensitive fields- For large objects,
JSON.stringify(obj, null, 0)is 20-30% faster thanJSON.stringify(obj, null, 2)
The Bottom Line
JSON formatting is a solved problem — the tools exist, the rules are clear, and the common errors are well-documented. The key is having a reliable formatter that works instantly without sending your data to a server.
Our JSON Formatter processes everything in your browser using native JSON.parse() and JSON.stringify(). Paste your JSON, see the result, copy and go. No accounts, no data collection, no nonsense.