JSON vs. JavaScript objects
JSON's syntax comes from JavaScript object literals, so the two look alike. A JavaScript object is a value inside a running program, written with the full language. JSON is a text format with a small, fixed grammar. Any JSON text is also a valid JavaScript expression, but a lot of JavaScript isn't valid JSON.
{
name: 'Ada Lovelace', // a comment
born: 1815,
tags: ['math', 'poetry',],
}The differences
| JavaScript | JSON | |
|---|---|---|
| Keys | Quotes are optional: {name: 1} | Always in double quotes: {"name": 1} |
| Strings | Single, double or backtick quotes | Double quotes only |
| Last item | A trailing comma is allowed | No trailing comma |
| Comments | // and /* */ | None |
| Values | Anything, including functions, undefined, NaN and dates | Objects, arrays, strings, numbers, true, false and null |
| Numbers | Also 0xFF, 1_000 and BigInt 10n | Decimal only, like 255 or 2.5e3 |
Converting between them
JSON.stringify(value) turns a JavaScript value into JSON text, and JSON.parse(text) reads JSON text back into a value. Some values don't survive the trip:
- Keys whose value is
undefinedor a function are left out. In an array, those values becomenull. NaNandInfinitybecomenull.- A
Datebecomes a string, like"2026-09-30T12:00:00.000Z", and stays a string afterJSON.parse. - A
MaporSetbecomes{}. - A BigInt throws a
TypeError.
To turn a JavaScript object literal you copied from code into JSON, quote every key, switch to double quotes, and remove comments and trailing commas.