What is JSON and Why Format It?
JSON (JavaScript Object Notation) is the most widely used data interchange format on the web. It is the backbone of REST APIs, configuration files, and data storage systems across virtually every programming language. Despite its simplicity, JSON has strict syntax rules that must be followed precisely. A single misplaced character can cause a parser to fail, breaking your entire application.
Properly formatted JSON is not just about correctness -- it is about readability and maintainability. Well-formatted JSON with consistent indentation makes it easy for developers to read, debug, and collaborate on data structures. This is where a good JSON formatter becomes invaluable, automatically handling indentation, sorting, and validation in seconds.
Mistake 1: Trailing Commas
This is arguably the most common JSON mistake developers make. In many programming languages like JavaScript, Python, and PHP, trailing commas after the last element in an array or object are perfectly valid. However, the official JSON specification (RFC 8259) explicitly forbids them.
// WRONG - Trailing comma after "blue"
{
"colors": ["red", "green", "blue"],
"count": 3,
}
// CORRECT - No trailing comma
{
"colors": ["red", "green", "blue"],
"count": 3
}
The fix is simple: remove the trailing comma before the closing bracket or brace. Using a JSON formatter will automatically strip trailing commas and ensure your JSON is syntactically valid.
Mistake 2: Unquoted Keys
In JavaScript object literals, you can write keys without quotes. But in JSON, all keys must be enclosed in double quotes. This is a strict requirement of the JSON specification, and parsers will reject any object with unquoted keys.
// WRONG - Unquoted keys
{
name: "Alice",
age: 30,
active: true
}
// CORRECT - All keys in double quotes
{
"name": "Alice",
"age": 30,
"active": true
}
This mistake is especially common for developers coming from JavaScript, where unquoted keys are standard practice. Always double-check that every key in your JSON object is wrapped in double quotes.
Mistake 3: Single Quotes Instead of Double Quotes
JSON requires double quotes for both strings and keys. Single quotes are not valid in JSON, even though they are commonly used in JavaScript and Python. This is one of the most frequent causes of JSON parse errors.
// WRONG - Single quotes
{
'name': 'Alice',
'city': 'New York'
}
// CORRECT - Double quotes
{
"name": "Alice",
"city": "New York"
}
If you are generating JSON from JavaScript code, be careful with template literals and single-quoted strings. Always use JSON.stringify() to produce valid JSON output rather than manually constructing strings.
Mistake 4: Comments in JSON
Unlike many programming languages and configuration formats, standard JSON does not support comments of any kind. Attempting to add // or /* */ comments will result in a parse error. This is a deliberate design decision by Douglas Crockford, the creator of JSON, to keep the format minimal and universal.
// WRONG - Comments are not allowed in JSON
{
"name": "Alice", // User's full name
"age": 30,
/* This is a multi-line
comment that will break JSON */
"city": "New York"
}
// WORKAROUND 1 - Use descriptive keys
{
"userName": "Alice",
"userAge": 30,
"userCity": "New York"
}
// WORKAROUND 2 - Use a "_comment" key
{
"_comment": "This object stores user profile data",
"name": "Alice",
"age": 30
}
If you need comments in your configuration files, consider using JSONC (JSON with Comments), which is supported by VS Code and TypeScript, or switch to YAML format. You can convert between formats using our JSON to YAML converter.
Mistake 5: Incorrect Nesting and Brackets
JSON relies on properly matched brackets and braces for its structure. Mismatched, missing, or extra brackets are a common source of errors, especially in large JSON files with deeply nested objects and arrays.
// WRONG - Missing closing bracket for "address"
{
"name": "Alice",
"address": {
"street": "123 Main St",
"city": "New York"
},
"phone": "555-1234"
}
// WRONG - Extra closing brace
{
"users": [
{"name": "Alice"},
{"name": "Bob"}
]]
}
// CORRECT - All brackets properly matched
{
"name": "Alice",
"address": {
"street": "123 Main St",
"city": "New York"
},
"phone": "555-1234"
}
The best way to catch nesting errors is to use a JSON visualizer that displays the structure graphically, making it immediately obvious when brackets are mismatched. A good formatter will also indent your JSON in a way that makes the nesting hierarchy visually clear.
How to Validate JSON
Validating your JSON before using it in production is essential. Here are several approaches:
- Online formatter: Paste your JSON into our free JSON formatter to instantly validate, format, and minify your data. It highlights errors and suggests fixes.
- JavaScript: Use
JSON.parse()to validate. Wrap it in a try-catch block to handle errors gracefully. - Python: Use
json.loads()from the standard library. It raises ajson.JSONDecodeErrorwith details about the problem. - Command line: Use
python -m json.tool file.jsonorjq . file.jsonto validate and format JSON files directly from your terminal.
// JavaScript validation example
try {
const data = JSON.parse(jsonString);
console.log("Valid JSON:", data);
} catch (error) {
console.error("Invalid JSON:", error.message);
}
JSON vs XML vs YAML: A Quick Comparison
While JSON is the most popular data format, it is worth understanding how it compares to alternatives:
| Feature | JSON | XML | YAML |
|---|---|---|---|
| Readability | Good | Verbose | Excellent |
| Comments | Not supported | Supported | Supported |
| File Size | Small | Large | Small |
| Data Types | Strings, numbers, booleans, null, arrays, objects | Everything is a string | Rich type support |
| Best For | APIs, web apps, config | Documents, SOAP APIs | Config files, DevOps |
Each format has its strengths. JSON excels in web applications and APIs, YAML is ideal for configuration files, and XML remains relevant in enterprise systems and document markup. Need to convert between formats? Try our JSON to YAML converter for quick, accurate transformations.
Conclusion
JSON formatting mistakes are frustrating but easily preventable. By understanding the five most common errors -- trailing commas, unquoted keys, single quotes, comments, and mismatched brackets -- you can save hours of debugging time. Use our free JSON formatter, JSON visualizer, and JSON to YAML converter to catch and fix these mistakes automatically, letting you focus on building great applications instead of chasing syntax errors.