Skip to content

JSON vs JavaScript Objects: Why Valid JS Fails JSON Parsing

Updated

JSON syntax was derived from JavaScript object literals, and the two look nearly identical — which is exactly why "but it works in my code!" is the most common reaction to a JSON parse error. JSON is a strict subset-like format defined by RFC 8259; JavaScript literals are far more permissive.

This guide lists every difference that bites in practice, with valid/invalid examples for each.

Quoting: double quotes only, keys included

JavaScript accepts single quotes, backticks, and unquoted identifier keys. JSON accepts exactly one form: double quotes around every string and every key.

Valid JavaScript, invalid JSON
{ name: 'Ada', 'role': `admin` }
Valid JSON
{ "name": "Ada", "role": "admin" }

No trailing commas, no comments

Modern JavaScript allows a trailing comma after the last element and supports // and /* */ comments. JSON forbids both. These two rules alone account for a huge share of "Unexpected token" errors when hand-editing config files.

Valid JavaScript, invalid JSON
{
  // primary admin
  "name": "Ada",
}
Valid JSON
{
  "name": "Ada"
}

Values JSON doesn't have

JSON has exactly seven value types: object, array, string, number, true, false, and null. JavaScript values with no JSON equivalent — undefined, NaN, Infinity, Date, RegExp, functions, BigInt — either throw or get silently transformed when you serialize.

JSON.stringify drops object properties whose value is undefined or a function, serializes NaN and Infinity as null, and converts Date objects to ISO strings. If a field mysteriously vanishes from your payload, an undefined value is the usual suspect.

JavaScript in, surprising JSON out
JSON.stringify({ a: undefined, b: NaN, c: new Date(0) })
// → {"b":null,"c":"1970-01-01T00:00:00.000Z"}  (a is gone)

Numbers are stricter too

JSON numbers can't have a leading + sign, a leading zero (01), a bare decimal point (.5 or 5.), or hex notation (0xFF) — all of which JavaScript tolerates. Very large integers are also a trap: JSON allows arbitrary precision in the text, but JavaScript parses numbers into 64-bit floats, silently rounding anything beyond Number.MAX_SAFE_INTEGER (9007199254740991).

How to check which rules you're breaking

Paste the text into the JSON validator: it applies the exact RFC 8259 rules and reports the first violation with line and column. If you genuinely need comments and trailing commas in config files, use a format that supports them — JSONC (VS Code settings), JSON5, or YAML — rather than bending JSON.

Try it now

Paste your JSON into the free toolkit — validate, beautify, minify, and explore it without your data ever leaving the browser.

Open the JSON Toolkit

Frequently asked questions

Is JSON a subset of JavaScript?

Essentially yes — since ES2019, every valid JSON text is also a valid JavaScript expression. The reverse is not true: most JavaScript object literals are not valid JSON because of quoting, trailing commas, comments, and JS-only values.

Why does JSON require double quotes?

The JSON standard (RFC 8259) defines strings as double-quoted only, to keep the grammar small and interoperable across every language. Parsers reject single quotes by design, not as a bug.

What happens to undefined when converting to JSON?

JSON has no undefined. JSON.stringify omits object properties whose value is undefined, and converts undefined array elements to null. If a key disappears during serialization, this is why.