JSON.stringify's Second Parameter Most Developers Skip

2025-04-01

Beyond JSON.stringify(obj)—replacer controls which keys export; space controls indentation. Handy for logs, mocks, and storage.

replacer array = whitelist

Pass ['id','name'] to emit only those keys at top level.

JSON.stringify(user, ["id", "name"]);

replacer function

Return undefined to omit; redact passwords; custom Date formatting.

JSON.stringify(obj, (k, v) => k === "password" ? undefined : v);

space

Third arg 2 or '\t' for pretty JSON; omit for compact logs.

toJSON

Objects can define toJSON(); Date uses ISO strings by default.