What is JSON?
JSON is a text format for storing and transporting data. It's based on JavaScript object syntax, but it's language-independent—every major programming language can read and write it.
json
{
"name": "Ada Lovelace",
"born": 1815,
"achievements": ["first programmer", "analytical engine"],
"active": false
}
It won the data format wars of the 2000s, beating XML for most web applications. Today, it's the default choice for APIs, config files, and data storage.
Why JSON Won
- Human readable - You can actually read it without a decoder ring
- Lightweight - No closing tags like XML (
</firstName>) - Universal - Every language has built-in JSON support
- Flexible - Nested objects, arrays, any structure you need
Data Types
JSON supports six data types:
| Type | Example | Notes |
|---|---|---|
| String | "hello" | Must use double quotes |
| Number | 42, 3.14, -17 | No quotes, no NaN/Infinity |
| Boolean | true, false | Lowercase only |
| Null | null | Lowercase only |
| Array | [1, 2, 3] | Ordered list |
| Object | {"key": "value"} | Key-value pairs |
That's it. No dates, no undefined, no functions, no comments.
Example: API Response
json
{
"status": "success",
"data": {
"user": {
"id": 12345,
"email": "dev@example.com",
"roles": ["admin", "user"],
"settings": {
"theme": "dark",
"notifications": true
}
}
},
"meta": {
"requestId": "abc-123",
"timestamp": 1706634000
}
}
Where You'll See This
- REST APIs - Request and response bodies
- Config files -
package.json,tsconfig.json - Databases - MongoDB, PostgreSQL JSON columns
- Local storage - Browser data persistence
- Message queues - Kafka, RabbitMQ payloads
Common Gotchas
⚠️Trailing Commas
JSON does NOT allow trailing commas. This is invalid:
{"name": "test",} — that comma before the closing brace will break parsing.
- Double quotes only -
'single quotes'are invalid. Always use"double quotes". - No comments - JSON has no comment syntax. Use JSONC or JSON5 if you need comments.
- No undefined - Use
nullinstead. - Keys must be strings -
{name: "test"}is invalid. Must be{"name": "test"}. - No trailing commas - Unlike JavaScript, the last item can't have a comma after it.
JSON vs Alternatives
| Format | Best For | Drawback |
|---|---|---|
| JSON | APIs, general data | No comments, verbose for config |
| YAML | Config files | Whitespace-sensitive, complex spec |
| XML | Legacy systems, documents | Verbose, harder to parse |
| TOML | Simple config | Less common, limited nesting |
| CSV | Tabular data | No nested structures |
In Code
javascript
// Parse JSON string to object
const data = JSON.parse('{"name": "test"}');
// Convert object to JSON string
const json = JSON.stringify({ name: "test" });
// Pretty print with indentation
const pretty = JSON.stringify(data, null, 2);
Try It
Format JSON"JSON: Because XML developers saw the light... and it was 50% smaller."