JSON is everywhere in modern software. REST APIs return JSON. NoSQL databases store JSON documents. Configuration files are increasingly JSON. Yet improperly formatted or invalid JSON causes silent failures, unhelpful error messages, and debugging sessions that consume hours. This guide explains what JSON formatting is, why it matters, the most common syntax errors and how to fix them, and the best practices that experienced developers follow.
What Is JSON Formatting?
JSON formatting refers to making JSON readable by humans through the addition of whitespace and line breaks. Minified JSON removes all unnecessary whitespace and appears as a single line. Formatted JSON adds indentation and line breaks to create a hierarchy that's easy for humans to scan.
Consider this minified JSON:
{"name":"Alice","age":30,"skills":["JavaScript","Python","Go"],"address":{"city":"San Francisco","state":"CA"}}
And the same data formatted with 2-space indentation:
{
"name": "Alice",
"age": 30,
"skills": [
"JavaScript",
"Python",
"Go"
],
"address": {
"city": "San Francisco",
"state": "CA"
}
}
The data is identical. Formatting has no effect on the data — it is purely cosmetic for human readability. Parsers and APIs accept both forms identically.
The JSON Data Model: Six Types
JSON defines exactly six types of values. Understanding each is critical for avoiding syntax errors:
1. String
Text values enclosed in double quotes (not single quotes). Strings must escape special characters with a backslash.
"hello" // valid
'hello' // INVALID - single quotes forbidden
"say \"hi\"" // valid - escaped double quote
"line1\nline2" // valid - newline escape sequence
2. Number
Integer or floating-point values. No quotes. No NaN, Infinity, or hexadecimal notation.
42 // valid
3.14 // valid
-5 // valid
1.5e-10 // valid - scientific notation
NaN // INVALID
0x2A // INVALID - hex notation forbidden in JSON
3. Boolean
Only true or false (lowercase, no quotes).
true // valid
false // valid
True // INVALID - must be lowercase
"true" // valid as a string, not a boolean
4. Null
The null value represents absence. Only null is valid (lowercase, no quotes).
null // valid
undefined // INVALID - not a JSON type
nil // INVALID
5. Object
Key-value pairs wrapped in curly braces. Keys must be strings in double quotes.
{"name":"Alice"} // valid
{name:"Alice"} // INVALID - keys must be quoted
{"age":30,"city":"NY"} // valid - multiple pairs
6. Array
Ordered list of values wrapped in square brackets. Values can be of any type (mixed types are allowed).
[1, 2, 3] // valid
["red", "green", "blue"] // valid
[42, "text", true, null] // valid - mixed types
[1, 2, 3,] // INVALID - trailing comma
The 7 Most Common JSON Syntax Errors
1. Trailing Commas
JSON forbids commas after the last element of an array or object. JavaScript allows them, which confuses many developers.
// WRONG
{
"name": "Alice",
"age": 30,
}
// CORRECT
{
"name": "Alice",
"age": 30
}
2. Single Quotes Instead of Double Quotes
JSON requires double quotes for strings and keys. Single quotes are invalid.
// WRONG
{'name': 'Alice', 'age': 30}
// CORRECT
{"name": "Alice", "age": 30}
3. Unquoted Property Keys
Object keys must always be strings in double quotes. This is valid JavaScript but invalid JSON.
// WRONG (valid JS, invalid JSON)
{name: "Alice", age: 30}
// CORRECT
{"name": "Alice", "age": 30}
4. Comments
JSON does not support comments. Lines starting with // or /* */ blocks are
invalid JSON (though they're valid in JavaScript).
// WRONG
{
"name": "Alice", // user's name
"age": 30 /* in years */
}
// CORRECT - use no comments
{
"name": "Alice",
"age": 30
}
5. Undefined Values
JSON has no undefined type. Use null instead.
// WRONG
{"name": "Alice", "email": undefined}
// CORRECT
{"name": "Alice", "email": null}
6. NaN or Infinity Numbers
JSON numbers must be finite. NaN and Infinity are JavaScript concepts, not JSON.
// WRONG
{"ratio": NaN, "max": Infinity}
// CORRECT - use null or numeric strings
{"ratio": null, "max": "Infinity"}
7. Missing Commas Between Elements
Each element in an array or each key-value pair in an object must be separated by a comma.
// WRONG
{"name": "Alice" "age": 30}
// CORRECT
{"name": "Alice", "age": 30}
Formatting JSON for Readability
The 2-space indentation standard is widely adopted by developers. Most JSON linters, formatters, and style guides enforce this convention for consistency across teams.
In JavaScript, the JSON.stringify() method includes a parameter for indentation:
const obj = {name: "Alice", age: 30};
const formatted = JSON.stringify(obj, null, 2);
console.log(formatted);
This produces beautifully indented JSON that's easy to read. Many IDEs and text editors have built-in formatters: VS Code (Shift+Alt+F), WebStorm, Sublime Text, etc.
Validating JSON Before Deployment
Validation checks two things: syntactic correctness (is it valid JSON?) and optionally semantic correctness (does it match an expected schema?).
Syntax Validation: Any JSON parser will validate syntax. If your JSON parses without error, it's syntactically valid. Use the DevTools JSON Formatter to validate instantly — it shows the line number and error message for any syntax mistakes.
Schema Validation: For stricter validation, use JSON Schema. A schema defines what fields must exist, their types, constraints, and relationships. Many programming languages have JSON Schema validators:
- Python: jsonschema library
- JavaScript/Node.js: ajv library
- Go: jsonschema package
- Java: org.json.schema library
Validate JSON before it reaches production APIs. A syntax error caught in development costs minutes. The same error caught in production costs hours of debugging and potential downtime.
Best Practices Summary
- Use 2-space indentation for consistency. Enforce it with linters (ESLint, Prettier, etc.)
- Always double-quote keys and strings. Never use single quotes or unquoted keys.
- Avoid trailing commas. Use a formatter to catch them automatically.
- Use null for absent values, never undefined or empty strings unless semantically meaningful.
- Validate before deploying. Catch syntax errors early, consider schema validation for structured data.
- Use a formatter in your IDE. Format on save to eliminate manual formatting work.
- Read error messages carefully. JSON parsers report the line and character of the first error. Start there.
Tools That Help
Ready to format and validate your JSON? Use the free DevTools JSON Formatter — no signup, no server upload, works offline. Features include:
- Instant JSON formatting with 2-space indentation
- Syntax validation with line numbers and error messages
- Interactive tree view for exploring nested structures
- Search and replace with regex support
- Import and export .json files
All processing is in your browser. Your JSON never leaves your computer.