JSON data types, syntax rules, nested structures, and common patterns
Double-quoted Unicode text. Escape special characters.
"Hello, World!"
"Line 1\nLine 2"
"Tab\there"
"Quote: \"word\""
"Unicode: \u0041"Integer or floating-point. No hex, no leading zeros.
42
-17
3.14
1.5e10
-2.5e-3
// Not valid JSON:
// 0xFF (hex)
// 01 (leading zero)Lowercase true, false, and null only
true
false
null
// Not valid: True, False, None, undefinedOrdered list of values in square brackets
[1, 2, 3]
["a", "b", "c"]
[true, null, 42, "mixed"]
[{"id": 1}, {"id": 2}]Unordered collection of key-value pairs in curly braces
{
"name": "Alice",
"age": 30,
"active": true
}Single quotes are not valid JSON
// Valid
{ "name": "Alice" }
// Invalid
{ 'name': 'Alice' } // single quotes!JSON does not allow trailing commas after the last item
// Valid
{ "a": 1, "b": 2 }
// Invalid
{ "a": 1, "b": 2, } // trailing comma!JSON does not support // or /* */ comments
// Invalid JSON:
{
// "comment": "this is not allowed",
"name": "Alice"
}
// Workaround: use a "_comment" key
{ "_comment": "describes the object", "name": "Alice" }Object keys must always be double-quoted strings
// Valid
{ "key": "value" }
// Invalid
{ key: "value" } // unquoted key
{ 42: "value" } // numeric keyParse a JSON string into a JavaScript value
const data = JSON.parse('{"name":"Alice","age":30}');
console.log(data.name); // "Alice"
// With reviver function
const parsed = JSON.parse(text, (key, value) => {
if (key === "date") return new Date(value);
return value;
});Convert a JavaScript value to a JSON string
JSON.stringify({ a: 1 }) // '{"a":1}'
JSON.stringify({ a: 1 }, null, 2) // pretty-print
// With replacer (filter/transform keys)
JSON.stringify(obj, ["name", "age"]) // only include these keys
JSON.stringify(obj, (k, v) => typeof v === "function" ? undefined : v)JSON is stricter than JavaScript object literals
// JavaScript allows: // JSON requires:
key: "value" "key": "value" (quoted)
'single quotes' "double quotes"
{ a: 1, } { "a": 1 } (no trailing comma)
undefined null (undefined not valid)
// comments OK // no comments!
function() {} not allowed (functions not valid)Standard REST API response envelope
{
"success": true,
"data": {
"id": 42,
"name": "Alice Johnson",
"email": "alice@example.com",
"createdAt": "2024-01-15T10:30:00Z",
"roles": ["admin", "editor"],
"profile": {
"avatar": "https://example.com/alice.jpg",
"bio": null
}
},
"meta": {
"requestId": "abc-123",
"timestamp": "2024-01-15T10:30:01Z"
}
}JSON configuration pattern for apps and tools
{
"name": "my-app",
"version": "1.0.0",
"settings": {
"theme": "dark",
"language": "en",
"notifications": {
"email": true,
"push": false
}
},
"database": {
"host": "localhost",
"port": 5432,
"name": "mydb"
},
"features": {
"beta": false,
"darkMode": true
}
}Common JSON structure for lists and collections
{
"users": [
{
"id": 1,
"name": "Alice",
"email": "alice@example.com",
"active": true
},
{
"id": 2,
"name": "Bob",
"email": "bob@example.com",
"active": false
}
],
"pagination": {
"total": 2,
"page": 1,
"limit": 20
}
}Validate JSON with a linter (jsonlint.com) before using it — one missing quote breaks the entire document
Use JSON.stringify(obj, null, 2) for readable pretty-printing when debugging
Beware of circular references — JSON.stringify throws on circular objects
Wrap JSON.parse() in a try/catch — invalid JSON throws a SyntaxError
Use structuredClone(obj) instead of JSON.parse(JSON.stringify(obj)) for deep cloning