JSON (JavaScript Object Notation) is the most widely used data format on the web. Every REST API, every configuration file, every data pipeline touches JSON at some point. Yet developers routinely lose time to formatting issues, validation errors, and syntax mistakes that are easy to avoid.
This guide covers everything you need to know about working with JSON effectively — from common errors to validation best practices.
JSON Syntax Rules (The Complete List)
JSON's syntax is intentionally minimal. Here are all the rules:
- Strings must use double quotes (
"), never single quotes - Keys must be strings (double-quoted)
- Values can be: string, number, boolean (
true/false),null, object, or array - No trailing commas — the last item in an object or array must not have a comma after it
- No comments —
//and/* */are not valid JSON - No undefined — use
nullinstead - Numbers cannot have leading zeros (except
0itself) or trailing decimal points - No hex numbers —
0xFFis not valid JSON - No single-line strings with newlines — use
\nfor newlines inside strings
The 5 Most Common JSON Errors
1. Trailing Commas
// INVALID — trailing comma after "age"
{
"name": "Alice",
"age": 30,
} This is valid JavaScript but invalid JSON. Remove the comma after the last property. Every JSON parser will reject this.
2. Single Quotes
// INVALID — single quotes
{ 'name': 'Alice' } JSON requires double quotes for all strings and keys. No exceptions.
3. Unquoted Keys
// INVALID — unquoted key
{ name: "Alice" } Every key must be a double-quoted string, even if it's a simple identifier.
4. Comments
// INVALID — comments not allowed
{
"name": "Alice", // user's name
"age": 30
} JSON does not support comments. If you need comments in config files, consider JSONC (JSON with Comments) or switch to YAML/TOML. Read our JSON vs YAML vs TOML comparison for guidance.
5. Missing Quotes Around String Values
// INVALID — unquoted string value
{ "status": active }
String values must be quoted: "status": "active". Without quotes, the parser interprets active as an undefined identifier.
JSON Formatting Best Practices
Use 2-Space Indentation
The JavaScript ecosystem has standardized on 2-space indentation for JSON. It's compact enough to keep deeply nested structures readable:
JSON.stringify(data, null, 2) Keep Keys Consistent
Pick one naming convention for keys and stick with it. camelCase is standard in JavaScript APIs. snake_case is common in Python and Ruby APIs. Mixing conventions makes code harder to work with.
Sort Keys for Diffs
When JSON files are committed to version control, sorted keys produce cleaner diffs:
JSON.stringify(data, null, 2) // unsorted — key order depends on insertion
JSON.stringify(data, Object.keys(data).sort(), 2) // sorted — stable diffs Minify for Production
Remove whitespace from JSON in API responses to reduce payload size. A typical JSON response is 30-50% smaller when minified:
JSON.stringify(data) // minified — no whitespace Use our JSON Formatter to switch between formatted and minified views instantly.
JSON Validation Methods
Online Validators
For quick checks, paste your JSON into an online validator. Our JSON Formatter & Validator highlights syntax errors with line numbers and runs entirely in your browser — your data stays private.
JSON Schema
For API contracts and configuration validation, JSON Schema is the standard. It defines the expected structure, types, and constraints:
{
"type": "object",
"required": ["name", "email"],
"properties": {
"name": { "type": "string", "minLength": 1 },
"email": { "type": "string", "format": "email" },
"age": { "type": "integer", "minimum": 0 }
}
} Libraries like Ajv (JavaScript), jsonschema (Python), and many others validate data against JSON Schema at runtime.
TypeScript Type Guards
In TypeScript projects, use type guards or libraries like Zod to validate JSON at runtime while maintaining type safety:
import { z } from 'zod';
const UserSchema = z.object({
name: z.string(),
email: z.string().email(),
age: z.number().int().positive().optional(),
});
const user = UserSchema.parse(jsonData); // throws if invalid Working with Large JSON
- Streaming parsers — for files too large to fit in memory, use streaming JSON parsers like
JSONStream(Node.js) orijson(Python) - JSON Lines (JSONL) — for datasets, use one JSON object per line instead of a giant array. This enables line-by-line processing.
- jq for CLI — the
jqcommand-line tool is indispensable for filtering, transforming, and extracting data from JSON files
JSON Alternatives
JSON isn't always the best choice:
- MessagePack / CBOR — binary JSON formats, 20-50% smaller, faster to parse
- Protocol Buffers — Google's binary format with schema enforcement. Used by gRPC.
- YAML / TOML — for human-edited configuration files where comments matter
- CSV — for tabular data, CSV is simpler and more compact. Use our JSON to CSV Converter to switch between formats.
Conclusion
JSON's simplicity is its greatest strength. Master the syntax rules, validate early, and choose the right tool for each job. For quick formatting and validation, our JSON Formatter & Validator is always one click away.