JSON Best Practices: Syntax to Production
JSON syntax rules, the pitfalls that break parsers, performance tuning and production conventions for frontend and backend developers.
What is JSON?
JSON (JavaScript Object Notation) is a lightweight data interchange format popularized by Douglas Crockford in 2001. Despite having "JavaScript" in its name, JSON has become a language-agnostic standard (RFC 8259) supported by virtually every programming language and platform.
JSON's core advantages lie in its simplicity and readability. Compared to XML, JSON has no verbose tags or namespace declarations. Compared to binary formats like Protobuf, JSON can be directly read and edited by humans. This makes JSON the go-to choice for Web APIs, configuration data, and lightweight data storage.
JSON Syntax Basics
JSON supports six data types:
- Strings: Wrapped in double quotes, e.g.
"hello". Note that JSON requires double quotes — single quotes are not valid. - Numbers: Integers or floats, e.g.
42,3.14,-100. NaN, Infinity, and hexadecimal are not supported. - Booleans:
trueorfalse(lowercase only). - null: Represents an empty value.
- Arrays: Ordered lists of values, wrapped in square brackets, e.g.
[1, 2, 3]. - Objects: Unordered key-value collections, wrapped in curly braces, e.g.
{"name": "DevKit", "version": 1}.
A common misconception is that JSON supports comments. Standard JSON does not support any form of comments. If you need to add notes, consider using JSONC (JSON with Comments) or keeping documentation in a separate file.
Common Pitfalls and Errors
1. Trailing Commas
{
"name": "DevKit",
"tools": ["JSON", "Base64",]
}
Trailing commas are legal in JavaScript object literals but cause parse errors in JSON. This is the most common error when copying JSON from JavaScript code.
2. Unquoted Keys
{
name: "DevKit"
}
JSON requires all keys to be wrapped in double quotes. Unquoted keys are JavaScript object literal syntax, not valid JSON.
3. Inconsistent Date Formats
JSON has no native date type. The common practice is to use ISO 8601 formatted strings:
{
"createdAt": "2026-10-15T08:30:00Z"
}
Always use ISO 8601 format with timezone information. Avoid using millisecond timestamps from Date.now() — while more compact, they're less readable and prone to timezone confusion across systems.
4. Oversized JSON Files
When JSON files exceed 10MB, the browser's JSON.parse() can block the main thread, causing page lag. For large datasets, consider:
- Using JSONL (JSON Lines) format — one JSON object per line, enabling streaming
- Implementing server-side pagination to transfer only necessary data
- Using binary formats like Protobuf or MessagePack for read-heavy scenarios
Production Best Practices
Always Validate Input
Never trust JSON submitted by clients. Use JSON Schema or similar validation libraries on the server side:
{
"type": "object",
"required": ["name", "email"],
"properties": {
"name": { "type": "string", "maxLength": 100 },
"email": { "type": "string", "format": "email" }
}
}
Use Consistent Naming Conventions
Maintain a uniform key naming style across your entire API. We recommend snake_case (e.g. created_at) or camelCase (e.g. createdAt), but never mix them. snake_case is more common in REST APIs, while camelCase dominates the JavaScript ecosystem.
Avoid Deep Nesting
Nesting beyond 3-4 levels significantly reduces readability and maintainability. If your data structure is too complex, consider splitting it into multiple related objects or using references (like foreign keys in databases).
Don't Put Sensitive Data in JSON
JSON itself provides no encryption or access control. Never store passwords, secrets, or personally sensitive information in JSON. If you need to transmit sensitive data, use HTTPS encryption and apply appropriate data masking on the server side.
JSON vs Other Formats
| Feature | JSON | XML | Protobuf | YAML |
|---|---|---|---|---|
| Readability | High | Medium | Low | High |
| Size | Medium | Large | Small | Medium |
| Parse Speed | Fast | Slow | Fastest | Medium |
| Type System | Weak | Weak | Strong | Weak |
| Comments | No | Yes | No | Yes |
| Best For | Web APIs | Config files | High-perf RPC | Config files |
Recommended Tools
These tools significantly boost productivity when working with JSON:
- JSON Formatting & Validation: Use the JSON Formatter to quickly beautify, compress, and validate JSON data
- JSON ↔ Protobuf Conversion: When converting between JSON and binary formats, use the JSON ↔ Protobuf tool
- JSON Path Queries: For large JSON documents, use JSONPath expressions to locate specific fields
Mastering JSON's specification and best practices is fundamental for every modern developer. We hope this guide helps you write more standardized, robust JSON data in your daily work.