JSON Syntax Overview
Learning Focus
JSON has exactly six value types and a handful of structural characters. Learn them once and you will never write invalid JSON again.
The Six Syntax Rules
| Rule | What It Means |
|---|---|
| 1. Key-value pairs | "key": value — keys must be double-quoted strings |
| 2. Comma-separated | Each pair or element is separated by , |
3. Objects use {} | Unordered set of key-value pairs |
4. Arrays use [] | Ordered list of values |
5. Strings use "" | Double quotes only — single quotes are invalid |
| 6. Six value types | string, number, object, array, boolean, null |
Structural Characters
{ } Begin / end object
[ ] Begin / end array
: Key-value separator
, Pair / element separator
" String delimiter
Complete Valid Example
{
"orderId": 9871,
"status": "pending",
"isPriority": false,
"discount": null,
"tags": ["express", "fragile"],
"customer": {
"name": "Alice Chen",
"email": "alice@example.com",
"address": {
"city": "Singapore",
"postalCode": "018989"
}
},
"items": [
{ "sku": "A100", "qty": 2, "price": 29.99 },
{ "sku": "B200", "qty": 1, "price": 149.00 }
]
}
String Escape Sequences
| Sequence | Character |
|---|---|
" | Double quote |
\ | Backslash |
\/ | Forward slash |
| Backspace |
| Form feed |
| ` | |
| ` | Newline |
| ` | |
| ` | Carriage return |
| Tab |
\uXXXX | Unicode escape (4 hex digits) |
Number Rules
{ "ok_integer": 42, "ok_negative": -17, "ok_float": 3.14 }
Invalid Formats
0xFF (hex), 0755 (octal), 007 (leading zero), +42 (plus), NaN, Infinity → all parse errors
Quick Validation
# jq — exit 0 = valid
echo '{"name":"Alice"}' | jq '.' > /dev/null && echo valid || echo INVALID
# Python
python3 -m json.tool data.json > /dev/null && echo valid
Concept Map
Concept Flow
JSON Document
└── Root Value
├── Object → Key-Value Pairs
└── Array → Ordered Elements
└── string / number / object / array / boolean / null
Common Pitfalls
| Error | Bad Example | Fixed |
|---|---|---|
| Single-quoted strings | {'key': 'val'} | {"key": "val"} |
| Trailing comma | {"a": 1,} | {"a": 1} |
| Unquoted key | {key: "val"} | {"key": "val"} |
undefined value | {"x": undefined} | {"x": null} |
| Comment inside JSON | {"a": 1 // note} | remove comment |
What's Next
- Previous: JSON vs XML vs YAML — Review format comparison.
- Next: JSON Data Types — Dive into each value type in detail.
- Section Overview — Return to the Introduction module index.