Learning navigation

Code with Animation

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.

JavaScript6 min readConcept 29 of 63

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.

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.

const value = { course: "React", lessons: 3 };

JSON.stringify(value, null, 2);
JavaScript valueJSON text or error

Ready to inspect a real result

Choose an example and run it. Stringify returns a string or undefined, or throws for unsupported input such as the supplied BigInt and circular fixtures. Parse can return objects, arrays or primitive JSON values.

Native operations run on the selected value or text. Object properties can be omitted; array positions can become null. Parsed-value previews label non-finite numbers and negative zero explicitly. Input is text, never JavaScript to evaluate.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1What happens if you use `JSON.stringify()` on an object that has a method (a function) attached to it?
  2. 2Which method turns JSON text back into a JavaScript value?
  3. 3Which of the following is valid JSON syntax?

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.

Done with this concept?

Mark it complete to track your progress. No login needed.