JSON.stringify Reference on MDN: What It Changes and When It Fits

A practical review of JSON.stringify as MDN documents it: what serialization and indentation change, what gets dropped, and when output stays usable.

People looking up the MDN entry for JSON.stringify usually want one of two things: the exact signature and edge cases, or confidence that the method will turn their data into readable, shareable text without losing anything important. This review covers both. It explains what the method does to your data, where the output stops being faithful to the input, and when a plain-text summary serves readers better than formatted JSON.

What the MDN reference covers

MDN's JavaScript reference pages generally follow a consistent structure: syntax, parameters, return value, exceptions, a description of behavior, examples, specification references, and a browser compatibility table. The JSON.stringify entry documents the method's three parameters (value, replacer and space), the TypeError cases, the toJSON hook, and how different value types are serialized. The method is defined by the ECMAScript specification, so MDN is a readable guide to standard behavior rather than the authority itself.

Evidence gap: this review was not checked against a specific dated revision of the MDN page, and it includes no quoted page text, figures or compatibility data. The behaviors below come from the ECMAScript standard and long-documented engine behavior. Check the live page for current wording and compatibility notes.

Is JSON.stringify suitable for your intended use?

Use it when you need a standard, machine-readable text form of plain data: objects, arrays, strings, finite numbers, booleans and null. Its main benefit is predictability. Any conforming JSON parser can read the output, and the space parameter gives you consistent indentation for human review.

The key tradeoff is that serialization is lossy for anything beyond plain data. Functions, undefined values and symbols are dropped or replaced. Dates become strings. Maps and Sets become empty objects. NaN and Infinity become null. Parsing the output does not restore the original types.

Plain text is the better choice when the audience is not technical, when the reader needs explanation rather than structure, or when the destination will collapse whitespace and leave a dense single line. In those cases, describe the relevant values in sentences or a table and attach the JSON separately for anyone who needs it.

What the transformation actually changes

In this context, "format" means adding whitespace. It has nothing to do with fonts, styling, reversal, mirroring or Unicode character substitution, and JSON.stringify does none of those. It walks a JavaScript value and builds a new string. The original value is not modified. The indentation and line breaks it adds are real characters stored in the string, not visual presentation applied by a renderer. Copying the string copies those characters too.

The space argument controls that whitespace. A number sets how many spaces to indent per level, capped at 10. A string such as a tab character is used as the indent, and only its first 10 characters count. Omitting the argument produces compact output with no added whitespace.

JSON.stringify({ a: 1, b: [1, 2] }, null, 2)

// result:
{
  "a": 1,
  "b": [
    1,
    2
  ]
}

The replacer argument filters or rewrites values during serialization. It can be a function called for each key and value, or an array of property names to keep. Objects with a toJSON method are serialized using that method's return value, which is why Date objects become ISO 8601 strings.

Values that are changed or lost

  • undefined, functions and symbols: omitted when they are object property values; become null inside arrays; a top-level call on one of them returns undefined instead of a string.
  • NaN, Infinity and -Infinity: serialized as null.
  • Date: serialized through toJSON as an ISO 8601 string, so it parses back as a string, not a Date.
  • Map and Set: their entries are not enumerable own properties, so they serialize as {} unless you convert them first.
  • Symbol-keyed and non-enumerable properties: ignored.
  • BigInt: throws a TypeError unless you supply a replacer or a toJSON method.
  • Circular references: throw a TypeError.
JSON.stringify({ u: undefined, f() {}, n: NaN, d: new Date(0) })
// '{"n":null,"d":"1970-01-01T00:00:00.000Z"}'

JSON.stringify([undefined, () => {}])
// '[null,null]'

Characters and escaping

Strings are wrapped in double quotes. Double quotes, backslashes and control characters such as newlines are escaped. Other characters, including accented letters and emoji, pass through unchanged. Multi-code-point sequences such as emoji built from several code points stay intact, because the method escapes characters rather than reordering or splitting them. Under the well-formed JSON.stringify behavior added in ES2019, a lone surrogate (half of a surrogate pair with no partner) is written as a \u escape instead of an invalid character, so the output remains valid UTF-16 text.

JSON.stringify("café 👍")
// '"café 👍"'

JSON.stringify("line1\nline2")
// '"line1\\nline2"'

JSON.stringify("\uD800")
// '"\\ud800"'

Key order follows standard property enumeration. Integer-like keys come first in ascending numeric order, followed by string keys in insertion order. If a consumer depends on a particular key order, check this behavior before you rely on it.

Will the result stay readable and usable after copying or sharing?

The output is ordinary text, so copying and pasting moves the exact characters, indentation included. Whether it stays readable depends on the destination. Code editors and monospaced code blocks generally keep indentation. Places that reflow text, collapse runs of spaces or use proportional fonts can make nested JSON hard to follow. This review did not test any specific destination, so check the place you plan to paste into before relying on it.

Reading order is not affected. JSON.stringify does not add bidirectional control characters or visually reorder anything, so the stored order matches the displayed order. Searching and editing work as they do with any text. One catch: escaped characters such as \n appear as two literal characters in the serialized string, so a search for an actual line break will not match them.

Accessibility gap: no documentation or test results about how specific screen readers announce indented JSON were available for this review, so no claim is made about any particular assistive technology. In general, deeply nested braces, brackets and quotation marks are a heavy load to hear read aloud. If people using assistive technology need the information, provide a plain-text or tabular summary next to the JSON.

If you paste data into a third-party online formatter or validator instead of running JSON.stringify locally, the data leaves your environment. A tool's appearance or marketing tells you nothing reliable about how it handles or keeps submitted data, so read its stated policy or format sensitive data locally.

Bottom line

JSON.stringify is a dependable, standard way to turn plain data into portable text, and the space parameter makes that text easy to review. It is the wrong tool if you need to preserve types such as Date, Map, Set, BigInt or functions, or if your readers need meaning rather than structure. For those cases, convert values explicitly with a replacer or toJSON, or give a plain-text explanation alongside the JSON.