What is JSON? A Beginner's Complete Guide
JSON explained from scratch — what it is, how to read and write it, the rules of valid JSON, common errors, and why it became the web's universal data format.
January 8, 2025
JSON is everywhere. It is the language APIs speak, the format configuration files use, and the structure databases store documents in. If you work with any web technology — as a developer, data analyst, or even a curious non-technical user — understanding JSON is essential. This guide explains JSON from the ground up.
What Does JSON Stand For?
JSON stands for JavaScript Object Notation. It was derived from JavaScript's object literal syntax by Douglas Crockford in the early 2000s and specified in RFC 4627 (2006), later updated by RFC 8259 (2017). Despite its JavaScript origins, JSON is completely language-independent — it can be read and written by Python, Java, Ruby, PHP, Go, Rust, and virtually any other programming language.
Why JSON Became the Standard
Before JSON, XML was the dominant data interchange format. XML is powerful but verbose: every data field requires both an opening and closing tag. JSON achieves the same goal with significantly less syntax, making payloads smaller and code simpler. Its structure maps naturally to objects and arrays in almost every programming language, making it intuitive to parse and generate.
When AJAX (Asynchronous JavaScript and XML) became popular, developers quickly discovered that JSON was far easier to work with than XML in browsers. The name AJAX stuck, but the "X" effectively became JSON.
JSON Data Types
JSON supports exactly six data types:
- String — Text enclosed in double quotes:
"hello world". Must use double quotes, not single quotes. - Number — Integer or floating-point:
42,3.14,-7,1.5e10. No special NaN or Infinity values. - Boolean —
trueorfalse(lowercase only). - Null —
null(lowercase) represents the absence of a value. - Array — An ordered list of values in square brackets:
[1, "two", true, null]. Values can be of any JSON type, including mixed types. - Object — An unordered collection of key-value pairs in curly braces:
{"name": "Alice", "age": 30}. Keys must be strings (in double quotes).
JSON Syntax Rules
JSON has strict syntax requirements that trip up beginners (and experienced developers using other languages that are more lenient):
- Strings must use double quotes — single quotes are invalid
- Object keys must be quoted strings — bare keys are invalid
- No trailing commas — the last item in an array or object must not have a comma
- No comments — JSON has no syntax for comments
- Numbers cannot have leading zeros (except
0itself) undefinedis not a valid JSON value (it is JavaScript-specific)- Special escape sequences in strings:
\n,\t,\\,\",\uXXXX
A Complete JSON Example
{
"user": {
"id": 12345,
"name": "Alice Johnson",
"email": "alice@example.com",
"isActive": true,
"score": 98.6,
"address": null,
"tags": ["admin", "editor"],
"preferences": {
"theme": "dark",
"notifications": true
}
}
}This example demonstrates all six JSON types within a nested structure.
Common JSON Errors
The most frequent JSON syntax errors are:
- Trailing comma:
{"a": 1, "b": 2,}— the comma after2is invalid - Single quotes:
{'key': 'value'}— must be double quotes - Unquoted keys:
{key: "value"}— keys must be in double quotes - Comments:
{"key": "value" // this is not allowed} - Undefined value:
{"key": undefined}— usenullinstead
Working with JSON in Code
JavaScript:
// Parse JSON string to object
const obj = JSON.parse('{"name":"Alice","age":30}');
console.log(obj.name); // "Alice"
// Convert object to JSON string
const json = JSON.stringify({ name: "Alice", age: 30 }, null, 2);
// Pretty-printed with 2-space indentationPython:
import json
# Parse JSON string
data = json.loads('{"name": "Alice", "age": 30}')
print(data["name"]) # Alice
# Convert to JSON string
json_str = json.dumps(data, indent=2)JSON vs. YAML vs. XML
- JSON vs. XML — JSON is terser and easier to parse in JavaScript. XML supports attributes, mixed content, and namespaces — useful for complex document formats. For APIs, JSON wins almost universally.
- JSON vs. YAML — YAML is a superset of JSON and supports comments, multiline strings, and more human-friendly syntax. YAML is popular for configuration files (Docker Compose, Kubernetes, GitHub Actions). JSON is better for data interchange and API payloads.
JSON5 and JSONC
Because JSON's strict rules (no comments, no trailing commas) are inconvenient for configuration files written by humans, extended formats have emerged. JSON5 allows single quotes, comments, trailing commas, and more. JSONC (JSON with Comments) allows comments only. Neither is valid JSON — they are supersets that require dedicated parsers (like the json5 npm package).
Summary
JSON is a lightweight, text-based data format that uses six value types (string, number, boolean, null, array, object) to represent structured data. It is language-independent, easy to read and write, and the universal standard for web API data exchange. The most common pitfalls are trailing commas, single quotes, and missing key quotes. Every developer should be fluent in reading and writing JSON — and in using tools to validate and format it.