JSON guide · 2 min read
JSON vs JavaScript Objects: The Differences That Cause Bugs
Understand why JavaScript object literals and JSON look similar but allow different syntax, values and runtime behavior.
JSON is text; a JavaScript object is a runtime value
JSON is a language-independent text representation. A JavaScript object exists in memory and can contain language features that have no JSON representation. Their similar braces make them easy to confuse, but the two are not interchangeable.
JSON requires double-quoted names and strings
JavaScript object literals can use unquoted identifier-like keys and single-quoted strings. Standard JSON requires property names and string values to use double quotes.
JavaScript object literal:
{ name: 'Ada', active: true }
Valid JSON:
{"name":"Ada","active":true}JavaScript has values JSON cannot represent directly
undefined, functions, symbols, BigInt and special numeric values such as NaN and Infinity are JavaScript concepts. JSON.stringify omits, transforms or rejects some of them depending on where they appear.
Comments and trailing commas are not JSON
Modern JavaScript allows comments and trailing commas in many contexts. Standard JSON does not. Configuration copied from JavaScript or TypeScript source can therefore fail a strict JSON parser even when it looks familiar.
Serialize instead of hand-building JSON text
JSON.stringify handles nested values, quotes and backslashes consistently. String concatenation becomes fragile as soon as user data contains punctuation that itself needs escaping.
const value = { name: 'Ada', note: 'She said \"hello\"' };
const json = JSON.stringify(value);Parse only when you actually have JSON text
JSON.parse is for strings containing JSON text. If a framework has already returned a JavaScript object, parsing again is unnecessary. Passing undefined or an already-parsed object is a common source of confusing errors.
Common questions
Frequently asked questions
Is every JavaScript object valid JSON?
No. JavaScript objects can contain syntax and values that JSON cannot represent, including functions, undefined and symbols.
Can JSON contain comments?
No. Standard JSON has no comment syntax.
Why should I use JSON.stringify?
It serializes supported JavaScript values with correct JSON quoting and escaping instead of relying on fragile hand-built strings.