JSON basics
The shape of JSON, how it maps to language types, and the easy mistakes that produce invalid JSON.
The JSON type system
JSON (JavaScript Object Notation) is a text format for structured data built from just two containers β objects {} and arrays [] β and a small set of scalar values: string, number, boolean, null.
| JSON | JavaScript | Python |
|---|---|---|
| object | Object | dict |
| array | Array | list |
| string | String | str |
| number | Number | int / float |
| true/false | boolean | bool |
| null | null | None |
Rules that break parsers
- Keys MUST be double-quoted strings.
- No trailing comma after the last element.
- No comments, no unquoted keys, no unquoted constants other than true/false/null.
- One value per document at the top level (object or array).
β οΈ
JSON is not JavaScript.
{a: 1} is valid JS but invalid JSON β the key needs quotes. This is the #1 cause of parse errors.{
"name": "Ada",
"age": 36,
"skills": ["math", "logic"],
"active": true,
"score": null
}Reading and writing it
// Parse (throws on invalid JSON)
const obj = JSON.parse(text);
// Serialize (replacer + indent for readability)
const text = JSON.stringify(obj, null, 2);import json
obj = json.loads(text) # parse
text = json.dumps(obj, indent=2) # pretty-printFAQ
What is the difference between JSON and a JS object?
JSON is a string format with stricter rules (quoted keys, no functions/comments). A JS object is an in-memory value.
How do I pretty-print minified JSON?
json.dumps(json.loads(text), indent=2) in Python, or JSON.stringify(obj, null, 2) in JS.
Related
CSV vs JSON UTF-8 and character sets
Last refreshed 2026-09-17.