iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

JSON Syntax

JSON syntax is a strict subset of JavaScript object literals. Knowing exactly what's allowed avoids parse errors.

The full grammar

AllowedExampleNOT allowed
Object{ "name": "Ada" }Unquoted keys, single quotes
Array[1, 2, 3]Trailing commas
String"hello"Single quotes
Number42, 3.14, -7, 1e5NaN, Infinity, leading zeros, hex
Booleantrue, false
nullnullundefined

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

VariantWhat it adds
JSONLOne JSON value per line — streamable logs, datasets.
JSON5Trailing 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 }

Test yourself

Q1. JSON keys must be…
Q2. Which is NOT valid JSON?
Q3. Trailing commas in JSON are…

Discussion

Loading…