# JSON: parse & stringify

> Learn the universal language of the web. Discover how to convert JavaScript objects into text for network transmission, and learn which data types get left behind.

Subject: JavaScript | Lesson 29 of 63 | 6 min read
Interactive lesson: https://codewithanimation.in/javascript/json
Previous: [Map, Set, WeakMap & WeakSet](https://codewithanimation.in/javascript/map-set-weakmap.md) | Next: [The Event Loop](https://codewithanimation.in/javascript/event-loop.md)

## The Universal Data Format

JSON (JavaScript Object Notation) is a lightweight text format used to store and transport data.

Because it is just text, it can be easily sent across the internet and read by almost any programming language, not just JavaScript.

## Serialization: The Two Methods

JavaScript provides a global `JSON` object with two critical methods:

`JSON.stringify(data)`: Converts a live JavaScript object or array into a flat JSON string.

`JSON.parse(string)`: Parses JSON text into a JavaScript value, including objects, arrays, strings, numbers, booleans or null.

## The Syntax Rules

JSON supports several value types. Its object and string syntax is stricter than a JavaScript object literal:

1. All keys MUST be wrapped in double quotes (`"name": "Link"`).

2. Strings must use double quotes, never single quotes or backticks.

3. No trailing commas are allowed at the end of lists or objects.

## Common mistake: The Missing Data Gotcha

JSON is a pure *data* format. It does not understand logic. Therefore, JSON only supports strings, numbers, booleans, arrays, objects, and null.

Undefined, function and Symbol values are omitted from object properties in the resulting JSON text. In array positions they become null; the original JavaScript value is not modified. A top-level undefined returns no JSON text. The supplied ordinary BigInt and circular fixtures throw a TypeError.

## Tip: Deep Cloning

JSON round-tripping only preserves JSON-compatible data. `structuredClone` supports additional structured-cloneable values, including undefined and cycles, but functions cause a DataCloneError. It is not a universal clone, and performance depends on the workload.

## Try it: Parse and serialize real values

Run native JSON operations, inspect omitted properties and array placeholders, and compare valid results with real parsing or serialization errors.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/json

## Check yourself

### Question 1

What happens if you use `JSON.stringify()` on an object that has a method (a function) attached to it?

- A. The method is silently omitted from the resulting JSON string.
- B. It throws a TypeError.
- C. The function is converted into a string of code.

Correct answer: A

JSON cannot represent logic or functions. If you try to stringify an object with a method, that property is simply dropped.

- Why not B: Incorrect. It doesn't crash; it just ignores the function.
- Why not C: Incorrect. JSON only supports pure data types, not logic.

### Question 2

Which method turns JSON text back into a JavaScript value?

- A. JSON.parse()
- B. JSON.stringify()
- C. Object.fromJSON()

Correct answer: A

`JSON.parse()` reads JSON text and returns the corresponding JavaScript value, which can also be a primitive or null.

- Why not B: Incorrect. `stringify` goes the other direction (object to string).
- Why not C: Incorrect. That method does not exist in JavaScript.

### Question 3

Which of the following is valid JSON syntax?

- A. {"name": "Mario"}
- B. {name: 'Mario'}
- C. {'name': 'Mario'}

Correct answer: A

JSON requires strict double quotes around both the keys and any string values.

- Why not B: Incorrect. Keys must be in double quotes, and strings must be in double quotes.
- Why not C: Incorrect. Single quotes are completely invalid in JSON.

## Remember this

- JSON is a universal text format for sending data across the network.
- `JSON.stringify()` converts JavaScript objects into a text string.
- `JSON.parse()` converts JSON text into a JavaScript value, including primitives and null.
- JSON requires double quotes for all keys and strings.
- Undefined, function and Symbol values are omitted in object properties but become null in array positions.
