What Is JSON?
JSON (JavaScript Object Notation) is a lightweight, human-readable format for storing and transmitting structured data. It was derived from JavaScript syntax but is completely language-independent — parsers exist for Python, Java, C#, Go, Ruby, PHP, Rust, and virtually every other programming language. Today, JSON is the dominant data format for REST APIs, configuration files, NoSQL databases, and web applications.
JSON was standardised in RFC 4627 (2006) and updated in RFC 8259 (2017). The core principle is simplicity: only a handful of data types, strict rules, and no ambiguity.
JSON Syntax: The Rules
Valid JSON must follow these rules precisely:
- A JSON document is a single value: either an object
{}or an array[](or a string, number, boolean, or null at the top level). - Strings must use double quotes — not single quotes.
- Object keys must be strings (in double quotes).
- No trailing commas after the last item in an object or array.
- No comments of any kind (unlike JavaScript, YAML, or TOML).
- No undefined, NaN, Infinity, or other JavaScript-specific values.
The Six JSON Data Types
1. String
"Hello, world!"
"It\'s a beautiful day"
"Line 1\nLine 2"
Strings go in double quotes. Special characters are escaped with backslash: \" for a double quote, \n for newline, \t for tab, \\ for a literal backslash.
2. Number
42
3.14159
-7
2.5e10
Numbers can be integers or floats, positive or negative. Scientific notation is supported. No hex (0xFF) or octal literals.
3. Boolean
true
false
Lowercase only. "True", "TRUE", "1", and "yes" are all invalid JSON booleans.
4. Null
null
Lowercase only. Represents the absence of a value.
5. Object
{
"name": "Alice",
"age": 30,
"active": true
}
An unordered collection of key-value pairs. Keys are strings; values can be any JSON type. Nested objects are valid.
6. Array
[1, "two", true, null, {"key": "value"}]
An ordered list of values. Arrays can contain mixed types, including other arrays and objects.
A Complete JSON Example
{
"user": {
"id": 12345,
"name": "Alice Johnson",
"email": "alice@example.com",
"active": true,
"roles": ["admin", "editor"],
"address": {
"city": "London",
"country": "UK"
},
"lastLogin": "2024-03-15T09:30:00Z",
"profilePicture": null
}
}
This demonstrates all six types: strings (name, email, city), numbers (id), booleans (active), arrays (roles), objects (nested address), and null (profilePicture).
Common JSON Errors and How to Fix Them
| Error | Invalid JSON | Fixed JSON |
|---|---|---|
| Single quotes | {'key': 'value'} | {"key": "value"} |
| Trailing comma | {"a": 1, "b": 2,} | {"a": 1, "b": 2} |
| Unquoted key | {key: "value"} | {"key": "value"} |
| Comment | {"a": 1 // note} | {"a": 1} |
| undefined value | {"a": undefined} | {"a": null} |
JSON vs XML: When to Use Each
JSON has largely replaced XML for API data exchange, but XML still dominates in some contexts:
- Use JSON for: REST APIs, web app data, NoSQL database documents, configuration files, inter-service communication.
- Use XML for: Document markup (HTML is XML-derived), enterprise system integration (EDI, SOAP), Android layouts, Maven configuration, contexts requiring namespaces or document type definitions (DTDs).
How to Validate and Format JSON
When working with JSON from external sources (API responses, copied data), always validate it before processing. The fastest way is to paste it into a JSON formatter that validates as it formats. Our free JSON Formatter and Validator does exactly this — paste your JSON, click Validate, and any syntax errors are highlighted with the exact line and character position.