JSON
JSON (JavaScript Object Notation) is the data format of web APIs. It's plain text, easy for humans to read and for every programming language to parse. If you've written a JavaScript object, you already almost know it.
The syntax
{
"id": 42,
"name": "Wireless Mouse",
"price": 24.99,
"inStock": true,
"discount": null,
"tags": ["electronics", "accessories"],
"dimensions": { "width": 6.2, "height": 3.8, "unit": "cm" },
"reviews": [
{ "user": "asha", "stars": 5 },
{ "user": "rahul", "stars": 4 }
]
}JSON has exactly six types:
| Type | Example |
|---|---|
| string | "hello" (always double quotes) |
| number | 42, -3.5, 1e6 |
| boolean | true, false |
| null | null |
| array | [1, 2, 3] |
| object | {"key": "value"} (keys are always double-quoted strings) |
Common mistakes
JSON is stricter than JavaScript:
{ name: "Asha" } ✖ keys need double quotes
{ 'name': 'Asha' } ✖ single quotes aren't allowed
{ "name": "Asha", } ✖ no trailing commas
{ "created": new Date() } ✖ no functions, dates or undefined
// comments ✖ JSON has no commentsJSON in JavaScript
JSON.stringify turns a value into JSON text; JSON.parse turns JSON text back into a value:
What doesn't survive the trip
- Dates become ISO 8601 strings (
"2026-11-20T09:30:00.000Z"). That's also the format to use in your APIs: unambiguous and sortable. Always send times in UTC. undefinedand functions disappear;NaNandInfinitybecomenull; Sets and Maps become{}.- Big numbers: JSON numbers are usually read as 64-bit floats, which can't hold integers above 2⁵³ exactly. Send large IDs (like Twitter's) and money amounts with many digits as strings, or store money as integer cents.
Invalid JSON
JSON.parse throws on invalid input. Any server parsing a request body must handle that:
Customising the output
Removing sensitive fields before sending is important, but don't rely on remembering it: build response objects explicitly ({ id, name }) so new database columns are never leaked by accident.
JSON in Python
Every language has a JSON library. Python's is built in:
API conventions
- Send
Content-Type: application/jsonand use UTF-8. - Pick one naming style and stick to it.
camelCase(createdAt) is common in JavaScript APIs,snake_case(created_at) in Python and Ruby ones. - Return objects at the top level, not bare arrays (
{"data": [...]}instead of[...]), so you can add fields like pagination info later without breaking clients. - Use
nullfor "no value" consistently, rather than sometimes leaving the field out. - JSON Lines (
.jsonl, one JSON object per line) is handy for logs and streaming many records.
Practice
- Write the JSON for a blog post with a title, an author object, a list of tags and a
publishedAtdate. - Write a function
safeParse(text)that returns{ ok: true, value }or{ ok: false, error }instead of throwing. - Give a
Userclass atoJSON()method that only exposesid,nameandemail.
1.Which of these is valid JSON?
2.What does JSON.parse(JSON.stringify({ when: new Date() })) give you for when?
3.Why send very large IDs as strings?