Skip to content
Fundamentals

JSON vs JavaScript Object: They Look Similar, but They're Not the Same

·4 min read
JSON vs JavaScript Object: They Look Similar, but They're Not the Same - Fundamentals guide from JSON Prettifier

Quick answer: A JavaScript object is an in-memory value in a running program; JSON is a text format for storing and transmitting data. JSON requires double-quoted keys and strings, forbids trailing commas, comments, undefined, functions, NaN, and Infinity, and supports only objects, arrays, strings, numbers, booleans, and null. Convert between the two with JSON.stringify() (object to text) and JSON.parse() (text to object).

Here is a mistake that catches even experienced developers when they are moving quickly:

const user = { name: 'Maya', active: true };

Someone copies that into a .json file and sees an error. It worked in JavaScript. Why not here?

Because a JavaScript object is a value in a program, while JSON is a standardized text format for representing data. The syntax overlaps, but JSON has stricter rules and supports fewer types.

Repair a copy of the failing payload with the JSON Fixer, then review the changes before using the result.

The easiest way to see the difference

This is a JavaScript object literal:

const user = {
  name: 'Maya',
  active: true,
  skills: ['PHP', 'JavaScript'],
};

This is valid JSON text representing similar data:

{
  "name": "Maya",
  "active": true,
  "skills": ["PHP", "JavaScript"]
}

See the differences? JSON requires double quotes around property names and strings, and it does not allow a trailing comma. The JSON text also does not include const user = because it is data, not a JavaScript statement.

The quick comparison

QuestionJavaScript objectJSON
What is it?An in-memory program valueA text data format
Must keys use double quotes?Not alwaysYes
Are single-quoted strings allowed?YesNo
Are trailing commas allowed?In object literals, yesNo
Can it contain undefined?YesNo
Can it contain functions?YesNo
Can it contain NaN, Infinity, or hex numbers like 0x1F?YesNo, only plain decimal numbers
Can it contain comments?In source code, yesNot in standard JSON
Typical useWork with values inside JavaScriptStore or transmit structured data

Convert objects into JSON with JSON.stringify()

When your application needs JSON text, let JavaScript produce it:

const user = { name: 'Maya', active: true };
const jsonText = JSON.stringify(user);

console.log(jsonText);
// {"name":"Maya","active":true}

Avoid hand-building JSON through string concatenation. It is easy to forget to escape a quote in a name such as Sam "The Builder", which would produce invalid JSON.

If readability matters, add indentation:

console.log(JSON.stringify(user, null, 2));

This only changes whitespace, not the meaning of the data.

Convert JSON text back into JavaScript with JSON.parse()

An HTTP response may give you JSON text:

const text = '{"name":"Maya","active":true}';
const user = JSON.parse(text);

console.log(user.name); // Maya

JSON.parse() expects a string containing valid JSON. It does not turn an existing JavaScript object into a more object-like object; passing an object coerces it to the string [object Object], which fails to parse.

If you call JSON.parse() on the wrong input, you can get errors such as Unexpected token or Unexpected end of JSON input. Our JSON.parse() guide covers these in more detail.

What gets lost during serialization?

A JavaScript object can include values JSON cannot represent:

const original = {
  name: 'Maya',
  nickname: undefined,
  greet() { return 'Hello'; },
  score: NaN
};

console.log(JSON.stringify(original));
// {"name":"Maya","score":null}

The undefined property and function are omitted; NaN (and Infinity) become null. If a field must be present but empty, use null explicitly; the null vs undefined guide explains why the two are not interchangeable in JSON.

That means JSON.stringify() is not a general-purpose object cloning method, as the deep clone guide shows. Date objects survive only as ISO 8601 strings (via their toJSON() method) and JSON.parse() will not turn them back into dates; see JSON date formats. Maps and sets serialize as {}, a BigInt throws a TypeError, and an object that references itself throws the circular structure error.

For API data, this is a useful warning: do not assume that everything in your JavaScript object will arrive unchanged at the other end.

A practical API example

A common fetch() request looks like this:

const payload = { email: 'maya@example.com', subscribed: true };

await fetch('/api/subscribers', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify(payload)
});

payload is an object. JSON.stringify(payload) creates JSON text for the request body. The server then parses that text and validates its fields.

Before sending a payload, you can use JSON Prettifier to inspect the JSON text. To learn the format from the beginning, see What is JSON?.

Bottom line

When you remember object = program value and JSON = data text, a surprising number of parsing and API mistakes stop being surprising.

Frequently Asked Questions

Is JSON the same thing as a JavaScript object?
No. A JavaScript object is a live value in memory; JSON is text that describes data. They look alike because JSON syntax was derived from JavaScript object literals, but JSON has stricter rules and fewer types.
Why does my JSON file reject single quotes?
Standard JSON only allows double quotes around keys and string values. Single quotes, backticks, and unquoted keys are JavaScript syntax, not JSON.
Can JSON contain functions or undefined?
No. JSON supports only objects, arrays, strings, numbers, booleans, and null. JSON.stringify() silently drops properties whose value is a function or undefined, and turns NaN and Infinity into null.
Is JSON a subset of JavaScript?
Syntactically, yes. Since ES2019 any valid JSON text is also a valid JavaScript expression. The reverse is not true, because JavaScript object literals allow unquoted keys, trailing commas, comments, and values that JSON cannot express.
Do I need to call JSON.parse() on response.json()?
No. The Fetch API's response.json() reads the body and parses it for you, resolving to a JavaScript value. Calling JSON.parse() on that value again throws a SyntaxError because it is no longer a string.

Check the JSON text, not the object

Paste the string from JSON.stringify() or an API response into JSON Prettifier to validate it, format it, or sort its keys before you compare two payloads.

Open JSON Prettifier