How to Format JSON Correctly
JSON (JavaScript Object Notation) is the de facto standard for data exchange on the web. APIs, configuration files, and NoSQL databases all rely on it. Yet a single misplaced comma or unclosed bracket can bring your entire application to a halt.
This guide covers how to format JSON correctly, the most common mistakes developers make, and how to use tools to catch errors before they hit production.
What is Proper JSON Formatting?
Proper JSON formatting means structuring your data so that it is both syntactically valid and human-readable. Valid JSON follows a strict set of rules:
- Data is in key/value pairs.
- Keys must be strings wrapped in double quotes.
- Values must be one of: string, number, object, array, boolean, or null.
- Strings must use double quotes — single quotes are invalid.
- No trailing commas are allowed after the last element in an object or array.
Common JSON Formatting Mistakes
1. Trailing Commas
JavaScript allows trailing commas. JSON does not.
{
"name": "EZTools",
"url": "https://eztools.pro",
}
That trailing comma after the last property will cause a parse error. Remove it.
2. Unquoted Keys
In JavaScript objects, unquoted keys are valid. In JSON, they are not.
{
name: "EZTools"
}
Keys must always be double-quoted strings.
3. Single Quotes Around Strings
JSON only recognizes double quotes.
{
"message": 'Hello, world!'
}
Always use double quotes for both keys and string values.
4. Comments in JSON
Standard JSON does not support comments. While some parsers like JSON5 allow them, standard JSON.parse() will throw an error if it encounters // or /* */.
5. Undefined Values
JSON does not have an undefined type. If a value is undefined, omit the property or use null.
Best Practices for Readable JSON
Use consistent indentation — typically 2 or 4 spaces. Group related properties together. Arrays of objects should be formatted with each object on its own line.
Example of well-formatted JSON:
{
"project": {
"name": "EZTools",
"version": "2.0.0",
"url": "https://eztools.pro"
},
"features": [
"privacy-first",
"client-side processing",
"offline capable"
],
"license": "MIT"
}
Validating and Formatting JSON Online
Manually hunting for syntax errors is inefficient. A good JSON formatter does three things:
- Validates the syntax and pinpoints exact error locations.
- Beautifies by adding consistent indentation and line breaks.
- Minifies by removing whitespace when you need compact output for production.
Our JSON Formatter handles all three tasks instantly. Paste your JSON, and it will either show you a clean, indented structure or highlight the exact line and character where the syntax breaks.
JSON in Real-World Development
APIs
REST and GraphQL APIs almost exclusively use JSON. When building or consuming an API, always validate payloads before sending them. malformed JSON is one of the most common causes of 400 Bad Request errors.
Configuration Files
package.json, tsconfig.json, and countless other config files are JSON. A formatting error in any of these can prevent your project from building.
NoSQL Databases
MongoDB stores documents as BSON (Binary JSON), but queries and imports often use standard JSON. Formatting errors in import files can lead to silent data loss if not caught early.
Summary
- Always use double quotes for keys and strings.
- Never leave trailing commas.
- Do not include comments in standard JSON.
- Validate your JSON with a formatter before deploying.
- Keep indentation consistent for readability and easier debugging.
Formatting JSON correctly is a small habit that prevents big headaches. Use a dedicated formatter to validate, beautify, and minify your data — your future self will thank you.