๐ JSON: The Complete Guide
From syntax to real-world use โ master the universal data format
What Is JSON?
JSON stands for JavaScript Object Notation. It's a lightweight, plain-text data interchange format. It represents structured data in a human-readable way and is the most universal data format in modern web development.
Here's a JSON snippet describing a user:
{
"name": "Alice",
"age": 28,
"email": "alice@example.com",
"tags": ["frontend", "React"]
}
This format is friendly to both humans and machines โ you can read it directly, and programs can parse it instantly.
Why Format and Validate JSON?
1. Minified JSON Is Unreadable
APIs often return minified (compact) JSON to save bandwidth:
{"code":200,"data":{"users":[{"id":1,"name":"Alice"}]}}
After formatting, it becomes instantly clear:
{
"code": 200,
"data": {
"users": [
{ "id": 1, "name": "Alice" }
]
}
}
Formatting = indentation + line breaks + syntax highlighting. You can see the data structure at a glance.
2. One Trailing Comma Can Break Everything
JSON syntax is strict. A single mistake causes parse failure. Common errors:
- โ Trailing comma after last element:
{"a": 1, "b": 2,} - โ Unquoted keys:
{name: "Alice"} - โ Single quotes instead of double:
{'name': 'Alice'} - โ Missing quotes or mismatched brackets
A validator tells you exactly what's wrong in real time, so you catch errors before deploying.
JSON Syntax Rules Quick Reference
| Rule | Description | Example |
|---|---|---|
| Structure | Key-value pairs; keys must be double-quoted | "name": "Alice" |
| Strings | Double quotes only (no single quotes) | "hello" |
| Numbers | Integer or float, no quotes | 42, 3.14, -1 |
| Booleans | Lowercase, no quotes | true, false |
| Null | Lowercase null | null |
| Arrays | Square brackets, comma-separated | [1, 2, 3] |
| Objects | Curly braces, comma-separated pairs | {"a": 1, "b": 2} |
| Nesting | Objects and arrays can nest arbitrarily | {"items": [{"id": 1}]} |
| Comments | โ JSON does not support comments | Use JSONC or YAML for comments |
Common JSON Use Cases
๐ API Debugging
Nearly every REST API today uses JSON. When you call an endpoint with curl or Postman, you get JSON back. The first thing you do with a wall of minified JSON is format it โ otherwise you'll never figure out which nesting level holds data.users[5].address.city.
โ๏ธ Configuration Files
package.json, VSCode's settings.json, ESLint's .eslintrc.json โ JSON is the default config format in the JavaScript ecosystem. A malformed config file can prevent your entire project from running.
๐ฆ Data Exchange
In a decoupled frontend-backend architecture, the frontend sends JSON to the backend and the backend returns JSON. When different languages (Java โ Python โ Go) need to share data, serializing to JSON strings is the standard approach.
๐๏ธ Databases
PostgreSQL has a native JSONB column type, MongoDB documents are essentially JSON (BSON), and Redis's JSON module makes JSON a standard cache format.
Format vs Minify: When to Use Which?
Format: During development, debugging, writing config files, sharing with colleagues โ you need readability.
Minify: For production transport, database storage, URL parameters โ you need to save bytes.
๐ Format your JSON now
Syntax highlighting, real-time validation, one-click minify, formatted copy
๐ Open JSON FormatterFAQ
Q: What's the difference between JSON and a JavaScript object?
JSON is a plain-text format; a JavaScript object is an in-memory data structure. JSON keys must be double-quoted, and you can't include functions, undefined, or NaN. Use JSON.parse() to convert a JSON string into a JavaScript object.
Q: Why do I get "Unexpected token" when parsing JSON?
99% of the time it's: a trailing comma, single quotes, unquoted keys, or invisible characters from copy-paste. Paste your content into a formatter โ it will highlight the error location.
Q: Does JSON support Chinese characters?
Yes, fully. JSON string values can contain any Unicode character โ Chinese, Japanese, emoji. No escaping needed: "message": "Hello world ๐".
Q: How deep can JSON nest?
No theoretical limit, but browsers and parsers usually enforce a default nesting depth (typically 100โ1000 levels). In practice, if your data is nested deeper than 5 levels, it's time to refactor.