JSON Syntax
JSON syntax is a strict subset of JavaScript object literals. Knowing exactly what's allowed avoids parse errors.
The full grammar
| Allowed | Example | NOT allowed |
|---|---|---|
| Object | { "name": "Ada" } | Unquoted keys, single quotes |
| Array | [1, 2, 3] | Trailing commas |
| String | "hello" | Single quotes |
| Number | 42, 3.14, -7, 1e5 | NaN, Infinity, leading zeros, hex |
| Boolean | true, false | — |
| null | null | undefined |
A small valid document
JSON
{
"name": "Ada Lovelace",
"active": true,
"skills": ["math", "code"],
"address": {
"city": "London",
"country": "UK"
},
"manager": null
}
What you can't have
- No comments (
// …,/* … */). - No trailing comma after the last item.
- No functions, dates, regex, undefined.
- Keys must be double-quoted strings.
- No hex (
0xFF) or octal (0777) numbers.
JSON Lines & JSON5
| Variant | What it adds |
|---|---|
| JSONL | One JSON value per line — streamable logs, datasets. |
| JSON5 | Trailing commas, comments, single quotes — convenient but non-standard. |
Tip: Always run JSON through
JSON.parse wrapped in try/catch at trust boundaries. Bad data is the most common source of runtime explosions.Example
Example
<!DOCTYPE html>
<html>
<body>
<p id="out"></p>
<script>
document.getElementById("out").textContent = "Hello from JSON Syntax!";
</script>
</body>
</html>
Try it Yourself »
Exercise
Pick the quote character JSON requires for keys and string values.
{
name
:
Ada
}
Double quotes.
Discussion
Loading…