Skip to content

JavaScript Tutorial JSON

JSON Syntax Rules

Lesson 131 of 258 2 min read By Deepak Paswan Updated Oct 4, 2026

JSON is strict. A tiny mistake makes the whole text invalid.

The six kinds of value

data.json
JSON
{
  "string": "text in double quotes",
  "number": 42.5,
  "boolean": true,
  "nothing": null,
  "array": [1, "two", false],
  "object": { "nested": "yes" }
}

JSON has no undefined, no functions, no dates (people store them as strings), no NaN, no comments.

The rules

  1. Keys must be strings in double quotes. { name: "Maya" } is not valid JSON.
  2. Strings use double quotes only. Single quotes are not allowed.
  3. No trailing commas. [1, 2, ] is invalid.
  4. No comments, neither // nor /* */.
  5. Numbers cannot have leading zeros, and use a dot for decimals.
script.js
JS
const valid = '{"name": "Maya", "tags": ["js", "css"]}';
console.log(JSON.parse(valid).tags[0]); // → js

const invalid = "{'name': 'Maya'}";
try {
  JSON.parse(invalid);
} catch (error) {
  console.log(error.name); // → SyntaxError
}

Common mistakes

code
TEXT
{ name: "Maya" }          ✗ unquoted key
{ "name": 'Maya' }        ✗ single quotes
{ "a": 1, }               ✗ trailing comma
{ "a": undefined }        ✗ undefined is not JSON
{ "a": 1 // note }        ✗ comment

Escaping inside strings

Special characters use backslashes: \" for a quote, \\ for a backslash, \n for a new line.

script.js
JS
console.log(JSON.stringify("She said \"hi\"")); // → "She said \"hi\""

Validating JSON

Paste suspicious JSON into a validator or the browser console. Most editors underline errors in .json files. Or let JSON.parse tell you, as in Handling JSON Errors.

Pretty JSON for reading

Whitespace is ignored, so JSON can be written on one line or neatly indented. JSON.stringify(value, null, 2) produces the indented version, see JSON.stringify.