JSON (JavaScript Object Notation) has become the universal language for data exchange on the web. Whether you're building APIs, configuring applications, or storing data, following JSON syntax best practices ensures your data is valid, readable, and maintainable.
Core JSON Syntax Rules
JSON has strict syntax requirements. Unlike JavaScript objects, JSON doesn't allow trailing commas, single quotes, or comments. Every key must be a double-quoted string, and values must be one of six types: string, number, object, array, boolean, or null.
{
"name": "John Doe",
"age": 30,
"isActive": true,
"email": null,
"tags": ["developer", "designer"],
"address": {
"city": "Austin",
"state": "TX"
}
}
Common JSON Syntax Errors
Trailing commas are the most frequent JSON error. The last item in an object or array must not have a comma after it:
// Invalid - trailing comma
{ "name": "John", "age": 30, }
// Valid
{ "name": "John", "age": 30 }
Single quotes don't work in JSON. Always use double quotes for strings and keys:
// Invalid
{ 'name': 'John' }
// Valid
{ "name": "John" }
Unquoted keys cause parsing failures:
// Invalid
{ name: "John" }
// Valid
{ "name": "John" }
Formatting Best Practices
Use consistent indentation. Two or four spaces are standard. Avoid tabs for maximum compatibility.
Keep nesting shallow. Deeply nested JSON becomes hard to read and maintain. If you're beyond 4-5 levels deep, consider restructuring your data.
Use meaningful key names. Keys like firstName are clearer than fn or x1. Your JSON is documentation.
Order keys logically. Group related fields together. Put identifiers first, metadata last.
Number Handling
JSON numbers don't have quotes and support integers, decimals, and scientific notation:
{
"integer": 42,
"decimal": 3.14159,
"scientific": 1.23e10,
"negative": -273.15
}
Avoid special values like NaN, Infinity, or leading zeros (except for decimals under 1). These aren't valid JSON.
Handling Special Characters
Strings must escape certain characters: backslash (\\), double quote (\"), and control characters. Unicode escapes work for any character:
{
"quote": "She said \"Hello\"",
"path": "C:\\Users\\Documents",
"emoji": "\u2764"
}
Validating Your JSON
Always validate JSON before deploying or transmitting it. Use our JSON Validator to catch syntax errors, see detailed error messages, and format your JSON properly.
Validation catches issues like:
- Missing or extra commas
- Mismatched brackets
- Invalid escape sequences
- Encoding problems
JSON Schema for Structure Validation
Beyond syntax validation, JSON Schema lets you define the expected structure of your data:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["name", "email"],
"properties": {
"name": { "type": "string" },
"email": { "type": "string", "format": "email" }
}
}
Performance Considerations
Minimize payload size for API responses. Remove unnecessary whitespace in production (minification) while keeping formatted JSON in development.
Use appropriate data types. Don't quote numbers or booleans. "true" is a string, not a boolean.
Consider alternatives for large datasets. JSON isn't optimal for large binary data or extremely long lists. Consider streaming JSON or binary formats when appropriate.
Key Takeaways
- Always double-quote keys and string values
- Never use trailing commas
- Validate before deploying
- Keep nesting shallow and keys meaningful
- Use our JSON Validator to catch errors early
Clean JSON syntax prevents debugging headaches and ensures smooth data exchange across your applications.