JSON (JavaScript Object Notation) has become the universal language of data exchange. APIs speak it, configurations use it, and databases store it. Yet despite its simplicity, JSON syntax errors remain one of the most common development headaches. A missing comma, an extra quote, or a trailing comma can break your entire application. This guide covers everything from JSON basics to advanced validation techniques.
JSON Syntax Fundamentals
| Type | Example | Notes |
|---|---|---|
| String | "Hello world" | Must use double quotes (not single) |
| Number | 42, 3.14, -17, 2.5e10 | No leading zeros, no hex, no NaN/Infinity |
| Boolean | true, false | Lowercase only |
| Null | null | Lowercase only |
| Object | {"key": "value"} | Keys must be double-quoted strings |
| Array | [1, 2, 3] | Comma-separated values |
2Common JSON Errors & How to Fix Them
| Error | Invalid JSON | Valid JSON |
|---|---|---|
| Trailing comma | {"a": 1, "b": 2,} | {"a": 1, "b": 2} |
| Single quotes | {'name': 'John'} | {"name": "John"} |
| Unquoted keys | {name: "John"} | {"name": "John"} |
| Missing comma | {"a": 1 "b": 2} | {"a": 1, "b": 2} |
| Comments | {"a": 1} // comment | {"a": 1} |
| Undefined value | {"a": undefined} | {"a": null} |
| Unescaped quotes | {"msg": "Say "hi""} | {"msg": "Say \"hi\""} |
Validate & Format Your JSON
Paste malformed JSON into our formatter to instantly find errors, fix syntax issues, and beautify the output.
Open JSON Formatter3JSON Formatting Best Practices
- **Indent with 2 spaces** – The most common standard; tabs are less portable
- **One property per line** – Except for short arrays like [1, 2, 3]
- **Consistent key ordering** – Alphabetical or logical grouping helps findability
- **No trailing commas** – Valid in JavaScript but invalid in JSON
- **Use null for missing values** – Not empty strings or undefined
- **Minify for production** – Remove whitespace when sending over the network
| Format | Pros | Cons |
|---|---|---|
| Minified JSON | Smaller file size, faster transfer, API standard | Unreadable, hard to debug |
| Pretty-Printed JSON | Human-readable, easy to debug, version control friendly | Larger file size (~30% more bytes) |
4Special Characters & Escaping
| Character | Escape Sequence | Description |
|---|---|---|
| " | \" | Double quote |
| \ | \\ | Backslash |
| / | \/ (optional) | Forward slash |
| Newline | \n | Line feed |
| Tab | \t | Horizontal tab |
| Carriage return | \r | Carriage return |
| Backspace | \b | Backspace |
| Form feed | \f | Form feed |
| Unicode | \uXXXX | Four hex digits |
Scenario
Store a file path and multi-line text in JSON
Solution
{"path": "C:\\Users\\John\\file.txt", "message": "Line 1\nLine 2"} — Backslashes are doubled; newlines become \n.
5JSON Validation Strategies
Validation Layers
Syntax validation
Is it valid JSON? Can it be parsed without errors? Tools like JSON.parse() will throw if not.
Type validation
Are values the expected types? Is "age" a number, not a string "25"?
Schema validation
Does the structure match expectations? Use JSON Schema to define required fields, patterns, and constraints.
Business logic validation
Do values make sense? Is the age between 0 and 150? Is the email format valid?
6JSON vs Other Data Formats
| Format | Strengths | Weaknesses | Best For |
|---|---|---|---|
| JSON | Simple, universal, fast parsing | Verbose, no comments, no dates | APIs, web data, configs |
| YAML | Human-readable, comments, anchors | Whitespace-sensitive, complex spec | Configuration files |
| XML | Attributes, namespaces, schemas | Verbose, complex, slow parsing | Legacy systems, documents |
| CSV | Compact, spreadsheet-friendly | No nesting, no types, escaping issues | Tabular data, exports |
| TOML | Readable, typed, sections | Less tooling, less known | Config files (Rust, Python) |
7Working with JSON in JavaScript
| Method | Purpose | Example |
|---|---|---|
| JSON.parse() | String → Object | JSON.parse('{"a":1}') → {a: 1} |
| JSON.stringify() | Object → String | JSON.stringify({a: 1}) → '{"a":1}' |
| JSON.stringify(obj, null, 2) | Pretty print | Adds 2-space indentation |
| JSON.stringify(obj, replacer) | Filter/transform | Custom serialization |
| JSON.parse(str, reviver) | Transform values | Custom deserialization |
Scenario
Parse JSON that might be invalid without crashing
Solution
try { const data = JSON.parse(input); } catch (e) { console.error("Invalid JSON:", e.message); } — Always wrap parse() in try-catch when handling user input or external data.
JSON Performance Tips
- **Minify in production** – Remove whitespace to reduce bandwidth
- **Use streaming parsers** – For large files, don't load everything into memory
- **Compress with gzip** – JSON compresses extremely well (often 80%+ reduction)
- **Consider JSON Lines** – One JSON object per line for log files and streaming
- **Use binary alternatives** – MessagePack, BSON, or Protocol Buffers for high-performance needs
- **Cache parsed objects** – Don't re-parse the same JSON repeatedly