JSON.stringify()

Beginner
5 min read
Updated: Nov 2026
3 examples

Definition and Usage

JSON.stringify() turns a JavaScript value into a JSON text string—ready for APIs, files, or localStorage.

It is the reverse of JSON.parse(). Optional replacer and space arguments let you filter properties and pretty-print the output.

Beginner path.

Stringify a plain object → try space for readable text → use a replacer when you need to drop fields. Pair with JSON.parse().

Syntax

JSON
JSON.stringify(value)
JSON.stringify(value, replacer)
JSON.stringify(value, replacer, space)
  • value The JavaScript value to convert (object, array, string, number, boolean, or null).
  • replacer Optional function (key, value) => …, or an array of property names to keep.
  • space Optional number or string for indentation (pretty-print). Often 2 or "\\t".

Special Cases

  • undefined Dropped on objects; becomes null in arrays.
  • Date Becomes an ISO-8601 string (via toJSON).
  • functions / Symbol Omitted on objects; null in arrays.
  • NaN / Infinity Serialized as null.
  • circular refs Throw TypeError — JSON has no cycles.
  • BigInt Throws unless you convert it first (for example to a string).

Output Shape

What you stringify should match the JSON shape your API or JSON Schema expects. Stringify only writes text; schema tools validate after someone parses it again.

JSON
const user = { name: "Ada", age: 36 };
const text = JSON.stringify(user);
const again = JSON.parse(text);
// again matches { "type": "object", "properties": { "name": …, "age": … } }

Examples

Use View Output for a quick result, or Try It Yourself to edit and run in the browser.

Example 1 — Basic Stringify

Turn an object into compact JSON text.

JSON
const user = {
  name: "Ada",
  age: 36,
  active: true
};

const text = JSON.stringify(user);
console.log(text);
console.log(typeof text);
Try It Yourself

How It Works

  1. Pass a plain object to JSON.stringify.
  2. You get a single-line JSON string.
  3. typeof of the result is "string".

Example 2 — Pretty-Print with space

Pass null for replacer and 2 for indentation.

JSON
const user = {
  name: "Ada",
  skills: ["math", "code"]
};

console.log(JSON.stringify(user, null, 2));
Try It Yourself

How It Works

  1. Second argument null means “no replacer”.
  2. 2 indents each level with two spaces.
  3. Use pretty text for logs and config; keep compact for APIs.

Example 3 — Replacer

Drop secrets (or any field) by returning undefined from a replacer.

JSON
const user = {
  name: "Ada",
  password: "secret",
  age: 36
};

const text = JSON.stringify(user, function (key, value) {
  if (key === "password") {
    return undefined;
  }
  return value;
});

console.log(text);
Try It Yourself

How It Works

  1. The replacer runs for each key/value pair.
  2. Returning undefined omits that property.
  3. Return every other value unchanged so it stays in the JSON.

Frequently Asked Questions

A string of JSON text for the input value. Without space it is usually one compact line. JSON.stringify(undefined) returns undefined, not a string.
Object properties with undefined are omitted. Array slots that are undefined become null. Functions and Symbols are treated the same way.
Pass a replacer: an array of property names to keep, or a function (key, value) that returns undefined to drop a property.
JSON cannot represent cycles. JSON.stringify throws TypeError if an object refers back to itself. Flatten the data or remove the cycle first.

Next: JSON Schema

Describe expected shape and validate JSON data.

JSON Schema tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

16 people found this page helpful