Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 39 ยท JSON in Depth

JSON.parse and JSON.stringify

Master JSON.parse and JSON.stringify: error handling, pretty printing, what gets dropped, deep copies, circular references and safe parsing patterns with runnable examples.

JavaScript gives you two functions for JSON in the global JSON object: JSON.parse turns text into a value, and JSON.stringify turns a value into text. Almost every web app uses them daily. This lesson shows the exact behavior, including the edge cases that bite.

JSON.parse

JSON.parse(text) returns the value described by text, or throws a SyntaxError. Always wrap untrusted input in try/catch.

Node.js Parsing and handling errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice that JSON.parse('null') is a perfectly valid result, so use an explicit fallback or a result object rather than testing the return value for falsiness.

JSON.stringify

JSON.stringify(value, replacer, space) has three parameters:

  • replacer: a function or an array of allowed keys (next lesson).
  • space: a number (0-10) or string used to indent output.
Node.js Indentation and what is dropped

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The rules, in short:

  • In objects, properties whose value is undefined, a function or a symbol are omitted.
  • In arrays, such values become null so the positions stay put.
  • NaN and Infinity become null; -0 becomes 0.
  • Only own, enumerable, string-keyed properties are written.
  • Map, Set and class instances lose their special nature: a Map becomes {}.
Node.js Map and Set need conversion

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Deep copies with a round trip

JSON.parse(JSON.stringify(x)) clones plain data, but it drops functions, turns dates into strings and fails on circular references. Prefer structuredClone(x) for real copies.

Node.js Round-trip copy versus structuredClone

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Circular references

An object that contains itself cannot be written as JSON, because the text would be infinite.

Node.js Circular structure error

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

(That seen set also flags a repeated, non-circular object; for a perfect solution, track the current path instead.)

Pretty or compact?

Use compact output (no space) for network traffic and logs that machines read. Use space of 2 for files people read, such as config and fixtures. Indentation can add 20-40% to the size.

Parsing safely

Parsed data is untrusted whenever it comes from a user or the network. Parsing itself is safe in JavaScript (it never runs code), but then you must validate the shape, and never merge it blindly into other objects. Parsing, security and tooling comes back to this, and JSON Schema is the proper way to validate.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does JSON.stringify({a: undefined, b: 1}) return?

    Object properties with undefined values are omitted.
  2. 2. What does JSON.stringify([undefined]) return?

    Inside arrays, unsupported values become null so positions are preserved.
  3. 3. What does JSON.parse throw on malformed input?

    Invalid JSON text raises a SyntaxError.
  4. 4. Which is a better way to deep copy data containing Date objects?

    structuredClone keeps Date objects; the JSON round trip converts them to strings.

Summary

  • JSON.parse returns a value or throws SyntaxError; wrap untrusted input in try/catch.
  • JSON.stringify drops undefined, functions and symbols in objects and nulls them in arrays.
  • Maps, sets and class instances need manual conversion; circular references throw.
  • Use structuredClone for deep copies and indent only for human-readable output.

Next: Replacers, revivers and toJSON