Lesson 2
JSON Syntax Basics
Brackets, commas, colons, and the rules every valid document must follow.
Every JSON document is built from a small set of punctuation rules. Once you internalize them, invalid files become much easier to fix.
Two structural forms
JSON values are either:
- An object — wrapped in
{}, containing key–value pairs - An array — wrapped in
[], containing an ordered list of values
A file can be a single object, a single array, or (less commonly) a single primitive value like "hello" or 42.
Objects: keys and colons
Inside an object, each entry looks like:
"key": value
Rules:
- Keys must be double-quoted strings
- A colon
:separates the key from its value - Commas
,separate entries—but never after the last one
{
"id": 1,
"label": "primary"
}
Arrays: ordered values
Arrays list values without keys:
["red", "green", "blue"]
Mixed types are allowed in one array (unlike some typed languages), though APIs often keep arrays homogeneous.
Nesting
Objects and arrays can contain each other without limit:
{
"users": [
{ "id": 1, "roles": ["admin", "editor"] },
{ "id": 2, "roles": ["viewer"] }
]
}
Whitespace
Spaces, tabs, and line breaks outside strings are insignificant. Formatting is for humans; parsers ignore extra whitespace.
Valid top-level shapes
| Valid | Invalid |
|---|---|
{ ... } | { key: "x" } (unquoted key) |
[ ... ] | 'string' (single quotes) |
"text", 42, true, null | { "a": 1, } (trailing comma) |
The next lesson covers each value type in detail.