SKU // 117

JSON Syntax

JSON has only a handful of rules, and a parser enforces every one of them. Break a single rule and the whole document is rejected. This lesson lists the rules, shows the mistakes that break them most often, and gives you a small function to test whether any text is valid JSON.

// The Basic Shape

JSON data is built from two structures: an object (curly braces holding named values) and an array (square brackets holding a list). Everything else is a plain value inside them.

{
  "title": "Dune",
  "pages": 412,
  "authors": ["Frank Herbert"],
  "inPrint": true
}

// Rule 1: Keys Are Double-Quoted Strings

Every key must be a string in double quotes. A JavaScript object lets you write title: "Dune", but JSON does not: it must be "title": "Dune". Single quotes are not allowed anywhere, for keys or for values.

// Rule 2: Strings Use Double Quotes

A string value is also written in double quotes. To put special characters inside, use a backslash escape:

Escape Means
\"a double quote
\\a backslash
\na line break
\ta tab
\u00e9a character by its code (here: the letter e with an accent)
const text = '"She said \\"hi\\"\\nThen left."';
console.log(JSON.parse(text));
She said "hi" Then left.

The doubled backslashes in the JavaScript line above are there for JavaScript itself: inside a JS string, \\n is a backslash followed by an n, which is exactly what JSON needs to see.

// Rule 3: Commas Between Items, Never After the Last

{ "a": 1, "b": 2 }       valid
{ "a": 1, "b": 2, }      invalid: comma after the last item
[1, 2, 3]                 valid
[1, 2, 3,]                invalid

JavaScript lets you leave a trailing comma, and many people add one by habit. JSON does not.

// Rule 4: No Comments, No Extras

JSON has no comment syntax at all: no //, no /* */. It also has no undefined, no functions, and no way to say "this value is a Date". Only the six kinds of values from the next lesson are allowed.

// Whitespace Does Not Matter

Spaces, tabs, and line breaks between pieces are ignored. These two documents are identical to a parser:

{"name":"Ava","age":16}

{
  "name": "Ava",
  "age": 16
}

Servers often send the compact version to save bytes. People usually format it with indentation so it is readable.

// The Top Level Can Be Any Value

A JSON document does not have to be an object. A whole document can be an array, a string, a number, true, false, or null:

console.log(JSON.parse("[1, 2, 3]"));
console.log(JSON.parse('"hello"'));
console.log(JSON.parse("42") + 1);
console.log(JSON.parse("null"));
[1, 2, 3] hello 43 null

// Testing Whether Text Is Valid

The easiest way to check JSON is to try to parse it. If the text breaks a rule, JSON.parse throws a SyntaxError, so a small try...catch function can answer yes or no (JS Errors covers try...catch):

function isValid(text) {
  try {
    JSON.parse(text);
    return true;
  } catch (error) {
    return false;
  }
}

console.log(isValid('{"name": "Ava"}'));
console.log(isValid("{name: 'Ava'}"));
console.log(isValid('{"a": 1,}'));
console.log(isValid('{"a": undefined}'));
console.log(isValid('// note\n{"a": 1}'));
true false false false false

// The Mistakes That Break It

Text Problem Fix
{name: "Ava"}Unquoted keyPut the key in double quotes
{"name": 'Ava'}Single-quoted stringUse double quotes
{"a": 1,}Trailing commaRemove the last comma
{"a": 1 "b": 2}Missing commaAdd a comma between pairs
{"a": undefined}undefined is not a JSON valueUse null or leave the key out
{"a": 1} // noteCommentRemove it: JSON has no comments
Case matters
The words true, false, and null must be lowercase. True and NULL are not JSON.

// Quick Check

Which of these is valid JSON?

  1. {name: "Ava"}
  2. {"name": 'Ava'}
  3. {"name": "Ava"}
  4. {"name": "Ava",}
Show answer
C. The key and the string both use double quotes and there is no trailing comma.

What does this log?

console.log(JSON.parse("42") + 1);
Show answer
43. A JSON document can be just a number. JSON.parse("42") gives the number 42, and 42 + 1 is 43.

Why is {"a": 1} // note not valid JSON?

Show answer
JSON has no comment syntax. Any // or /* */ makes the whole text invalid.

// Try It

  1. Click Open Playground and run it. Predict each true or false before you look
  2. Fix this broken JSON until isValid says true: {name: 'Ava', "grade": 10,}
  3. Write a valid JSON array that contains a number, a string, and null, then parse it
  4. Add a comment to valid JSON and confirm the parser rejects it
// Video Walkthroughs
Last updated: Oct 11, 2026, 5:23 PM ET