JSON Rules

Beginner
5 min read
Updated: Nov 2026
3 examples

Definition and Usage

JSON is a strict text format. A few hard rules decide whether JSON.parse accepts your document or throws SyntaxError.

Those rules are tighter than JavaScript object literals: always double-quote keys and strings, never leave a trailing comma, and stick to the six value types.

Beginner path.

Learn the checklist below, compare valid vs invalid, then run the examples. Next: JSON Comments.

Core Rules

  • Quoted keys Object keys must be double-quoted strings — "name", not name.
  • Double quotes Strings use " only. Single quotes are invalid in JSON.
  • Commas Separate properties and array items with commas — but not after the last one.
  • No trailing comma {"a":1,} and [1,2,] fail.
  • No comments // and /* */ are not allowed in strict JSON.
  • Six types only string, number, object, array, boolean, null — no undefined, Date, or function.

Valid vs Invalid

Same idea: one document follows the rules; the other looks like a JS literal.

Valid JSON
JSON
{
  "name": "Ada",
  "skills": ["math", "code"],
  "active": true
}

Quoted keys, double-quoted strings, commas only between items.

Invalid — JS-style
Not JSON
{
  name: 'Ada',
  skills: ['math', 'code',],
  active: true,
}

Unquoted keys, single quotes, and trailing commas break JSON.parse.

Rules Come First

JSON Schema describes shape and types after the text is valid JSON. If the document fails the rules above, parsers never reach schema checks.

Example: first the payload must parse; then a schema can require name and age.

JSON
{
  "type": "object",
  "required": ["name", "age"],
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer" }
  }
}

Examples

Use View Output for a quick result, or Try It Yourself to edit and run in the browser.

Example 1 — Follow the Rules

Valid JSON parses cleanly into an object.

JSON
const text = `{
  "name": "Ada",
  "skills": ["math", "code"],
  "active": true
}`;

const data = JSON.parse(text);
console.log(data.name);
console.log(data.skills.length);
console.log(data.active);
Try It Yourself

How It Works

  1. Keys and strings use double quotes.
  2. Commas sit only between items.
  3. JSON.parse returns a normal object.

Example 2 — Trailing Comma Fails

A comma after the last property is a classic mistake.

JSON
const bad = '{"name":"Ada","age":36,}';

try {
  JSON.parse(bad);
  console.log("Parsed OK");
} catch (err) {
  console.log(err.name);
  console.log("Trailing comma is invalid");
}
Try It Yourself

How It Works

  1. The text ends with ,}.
  2. Strict JSON forbids that trailing comma.
  3. catch reports SyntaxError.

Example 3 — Quotes Matter

Single-quoted keys/strings look fine in JS—not in JSON.

JSON
const ok = '{"name":"Ada"}';
const bad = "{'name':'Ada'}";

console.log(JSON.parse(ok).name);

try {
  JSON.parse(bad);
} catch (err) {
  console.log(err.name + ": use double quotes");
}
Try It Yourself

How It Works

  1. Double quotes around keys and strings succeed.
  2. Single quotes are not JSON string delimiters.
  3. Fix the quotes—then parse again.

Frequently Asked Questions

No. After the last property in an object or the last element in an array, do not place a comma. JSON.parse rejects trailing commas.
No. Keys must be strings in double quotes. Unquoted keys are fine in JavaScript object literals, not in JSON.
No. // and /* */ are not part of JSON. See the JSON Comments page for safe alternatives like a _comment field.
JSON is stricter text: double-quoted keys and strings, no trailing commas, no undefined, and only the six JSON value types. JS literals allow more shortcuts.

Next: JSON Comments

See why comments break JSON—and what to use instead.

JSON Comments tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful