JavaScript JSON.stringify() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
JSON

Definition

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.

JavaScript
JSON.stringify(value);
JSON.stringify(value, replacer);
JSON.stringify(value, replacer, space);
CallWhat it does
JSON.stringify(value)Converts the value to a compact JSON string.
JSON.stringify(value, replacer)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

ParameterDescription
valueRequired. The value to convert: object, array, string, number, boolean, or null.
replacerOptional. An array of property names to keep, or a function (key, value) that returns the new value (or undefined to drop it).
spaceOptional. A number of spaces (up to 10) or a short string used as indent. Makes the output easier to read.

Return Value

Return ValueDescription
JSON stringText that follows the JSON rules for the given value.
undefinedReturned 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);
Try It Yourself

How It Works

  1. user is a normal object.
  2. JSON.stringify(user) builds one line of JSON text.
  3. The result is a string, not an object.

Example 2 — Pretty Print with space

Pass 2 as the third argument to indent the output.

JavaScript
const order = {
  id: 7,
  items: ["tea", "cake"]
};

const pretty = JSON.stringify(order, null, 2);
console.log(pretty);
Try It Yourself

How It Works

  1. null means “no replacer”.
  2. 2 means two spaces of indent on each level.
  3. Great for logs and config files people will read.

Example 3 — Keep Only Some Keys

Pass an array of key names to keep only those properties.

JavaScript
const user = {
  name: "Ann",
  age: 20,
  password: "secret"
};

const safe = JSON.stringify(user, ["name", "age"]);
console.log(safe);

const hidden = JSON.stringify(user, function (key, value) {
  if (key === "password") {
    return undefined;
  }
  return value;
});
console.log(hidden);
Try It Yourself

How It Works

  1. An array replacer lists the keys you want to keep.
  2. A function replacer can return undefined to drop a key.
  3. Both ways remove password from the JSON string.

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.

ES5 Standard since
Google Chrome All modern · Desktop & Mobile
Full support
Microsoft Edge All modern · Chromium and legacy
Full support
Mozilla Firefox All modern · Desktop & Mobile
Full support
Apple Safari All modern · macOS & iOS
Full support
Opera All modern · Chromium based
Full support
Samsung Internet Recent versions · Android
Supported
Android WebView Supported · Chromium based
Supported
Node.js 0.10+ · Server runtime
Supported
Deno Supported · JavaScript runtime
Supported
Bun Supported · JavaScript runtime
Supported
Internet Explorer IE 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.

Next: JSON.rawJSON()

Embed already-valid JSON text without stringifying it again.

JSON.rawJSON() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful