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, andInfinity, and supports only objects, arrays, strings, numbers, booleans, andnull. Convert between the two withJSON.stringify()(object to text) andJSON.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
| Question | JavaScript object | JSON |
|---|---|---|
| What is it? | An in-memory program value | A text data format |
| Must keys use double quotes? | Not always | Yes |
| Are single-quoted strings allowed? | Yes | No |
| Are trailing commas allowed? | In object literals, yes | No |
Can it contain undefined? | Yes | No |
| Can it contain functions? | Yes | No |
Can it contain NaN, Infinity, or hex numbers like 0x1F? | Yes | No, only plain decimal numbers |
| Can it contain comments? | In source code, yes | Not in standard JSON |
| Typical use | Work with values inside JavaScript | Store 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.