← All concepts
Beyond the Course

JSON Serialization

JSON.stringify and JSON.parse move data to and from text, but not every value survives the trip.

Definition

JSON.stringify turns a JavaScript value into a JSON text string, and JSON.parse turns that text back into a value. The round trip is lossy in specific, predictable ways. Inside an object, a property whose value is undefined, a function, or a symbol is dropped entirely from the output; the same values inside an array are not dropped but replaced with null, because arrays must keep their length and index positions. Numbers that JSON cannot represent, NaN and Infinity, also serialize to null. Dates get special handling: JSON.stringify calls a Date's toJSON method, which returns an ISO string, so a Date becomes a plain string in the JSON text. Parsing that string back with JSON.parse does not turn it back into a Date; it stays a string unless you convert it yourself. Circular references, where an object points back to itself through some chain of properties, throw a TypeError instead of producing output, since there is no way to represent an infinite structure as finite text.

Examples

const data = {
  name: "Ada",
  score: undefined,
  greet: function () {},
  joined: new Date("2024-01-01"),
};
JSON.stringify(data);
// '{"name":"Ada","joined":"2024-01-01T00:00:00.000Z"}'

score and greet are dropped because undefined and functions are not valid JSON values. joined survives, but only as the ISO string its toJSON method returns.

JSON.stringify([1, undefined, function () {}, NaN]);
// '[1,null,null,null]'

Inside an array, values that would be dropped from an object become null instead, so the array keeps its original length.

const restored = JSON.parse(JSON.stringify({ joined: new Date() }));
typeof restored.joined; // "string", not a Date

Parsing never re-creates a Date automatically; the round trip leaves you with the ISO string you serialized.

Common mistakes

  • Assuming a Date survives JSON.parse(JSON.stringify(x)); it comes back as a plain string, not a Date instance.
  • Forgetting that undefined and function properties silently disappear from objects instead of raising an error.
  • Serializing an object that contains a circular reference and being surprised by a thrown TypeError instead of truncated output.

Key takeaways

  • undefined, functions, and symbols are dropped from objects but become null inside arrays.
  • NaN and Infinity are not valid JSON numbers, so they also serialize to null.
  • Dates serialize to ISO strings and do not automatically revive as Dates on parse.
  • Circular references throw instead of producing output.
Check your understanding

What does JSON.stringify({ a: undefined, b: [undefined] }) produce?

Where you see this

  • Sending a JavaScript object to a server as the body of a fetch request.
  • Saving structured data to localStorage, which only stores strings.
  • Deep-cloning a plain object or array with JSON.parse(JSON.stringify(x)), knowing it drops functions and undefined.
  • Debugging why a date field comes back as a string after loading data from an API or storage.

Practice this

Further reading

Previous
Iterators and Generators
Next
Arrays & Two-Pointers