In JavaScript, JSON.stringify() turns a value into a JSON string. You use it to send data to a server, save it in localStorage, or print a clear copy of an object.
You can pass a replacer to keep or change selected keys, and a space value to add indentation. Values that JSON cannot represent (functions, undefined, symbols) are skipped or become null in arrays.
Syntax
Call JSON.stringify on the value. Add replacer and space only when you need them.
Filters or changes keys and values while converting.
JSON.stringify(value, null, 2)
Pretty-prints with 2 spaces of indent. Pass null when you only want spacing.
Pair with JSON.parse() to turn the string back into a value.
Library declarations
JavaScript
// ECMAScript 5 (ES5)
JSON.stringify(value)
JSON.stringify(value, replacer)
JSON.stringify(value, replacer, space)
// value : any JavaScript value
// replacer : optional array of keys, or function (key, value) => any
// space : optional number (0–10) or string for indentation
// returns : a JSON string, or undefined for some top-level values
Parameters
Parameter
Description
value
Required. The value to convert: object, array, string, number, boolean, or null.
replacer
Optional. An array of property names to keep, or a function (key, value) that returns the new value (or undefined to drop it).
space
Optional. A number of spaces (up to 10) or a short string used as indent. Makes the output easier to read.
Return Value
Return Value
Description
JSON string
Text that follows the JSON rules for the given value.
undefined
Returned for a top-level undefined, function, or symbol.
TypeError (thrown)
Thrown when the value has a circular reference (an object that points back to itself).
JavaScript Examples
Example 1 — Basic JSON.stringify()
Convert an object to a compact JSON string.
JavaScript
const user = { name: "Ann", age: 20 };
const text = JSON.stringify(user);
console.log(text);
console.log(typeof text);
An array replacer lists the keys you want to keep.
A function replacer can return undefined to drop a key.
Both ways remove password from the JSON string.
Compatibility
Browser & Runtime Support
JSON.stringify() is part of ES5. Every current browser and Node.js has it. No import is needed.
✓ ES5 · Widely supported
JSON.stringify()
Supported in Chrome, Edge, Firefox, Safari, Opera, and Node.js for many years. Safe for everyday web and server code.
ES5Standard since
Google ChromeAll modern · Desktop & Mobile
Full support
Microsoft EdgeAll modern · Chromium and legacy
Full support
Mozilla FirefoxAll modern · Desktop & Mobile
Full support
Apple SafariAll modern · macOS & iOS
Full support
OperaAll modern · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebViewSupported · Chromium based
Supported
Node.js0.10+ · Server runtime
Supported
DenoSupported · JavaScript runtime
Supported
BunSupported · JavaScript runtime
Supported
Internet ExplorerIE 8+ · Legacy
Supported
JSON.stringify()Excellent
Bottom line: Safe to use everywhere. Use space for readable logs, and a replacer when you must hide keys.
Frequently Asked Questions
It turns a JavaScript value into a JSON string. You use that string to send data to an API, save it in localStorage, or print it for debugging.
value is what you want to convert. replacer is optional: an array of keys to keep, or a function that can change or drop values. space is optional and adds indentation for readable output.
A JSON string for most values. It returns undefined for undefined, functions, and symbols at the top level. Those values are also skipped inside objects.
Pass a number or a string as the third argument, for example JSON.stringify(data, null, 2). That adds line breaks and two spaces of indent.
Date objects become ISO date strings. Functions, undefined, and symbols are dropped from objects. Use a toJSON method or a replacer if you need a custom shape.
No. If an object points back to itself, JSON.stringify() throws a TypeError. Remove the cycle or use a custom replacer first.