JSON.stringify Tutorial: JavaScript Objects to JSON

JSON.stringify() converts JavaScript values into JSON text. It looks simple, but the details matter when you need readable output, want to hide private fields, or hit values that JSON cannot represent.

Basic JSON.stringify usage

Pass a JavaScript object, array, string, number, boolean, or null toJSON.stringify(). The return value is a JSON string.

const user = {
  id: 42,
  name: 'Alice',
  active: true,
  roles: ['admin', 'editor']
}

const json = JSON.stringify(user)
console.log(json)
// {"id":42,"name":"Alice","active":true,"roles":["admin","editor"]}

The output has no extra whitespace by default. That is ideal for network payloads and storage, but hard to read during debugging.

Pretty print JSON with spacing

The third argument controls indentation. Use 2 spaces for readable JSON.

JSON.stringify(user, null, 2)

// {
//   "id": 42,
//   "name": "Alice",
//   "active": true,
//   "roles": [
//     "admin",
//     "editor"
//   ]
// }

You can also pass a string for indentation, but spaces are the safest default for JSON files committed to a repository.

Use a replacer array to keep selected fields

The second argument can be an array of property names. Only those keys are included.

const user = {
  id: 42,
  name: 'Alice',
  email: 'alice@example.com',
  passwordHash: 'secret'
}

JSON.stringify(user, ['id', 'name'], 2)

// {
//   "id": 42,
//   "name": "Alice"
// }

This is useful for small allowlists, but it applies by property name at every nesting level. For conditional logic, use a replacer function instead.

Use a replacer function to transform values

A replacer function runs for every key and value. Return undefined to omit a property from objects.

const safeJson = JSON.stringify(user, (key, value) => {
  if (key === 'passwordHash') return undefined
  if (key === 'email') return String(value).toLowerCase()
  return value
}, 2)

Replacers are good for removing secrets, normalizing values, and creating stable debug output. Do not use them as your only security layer for API responses; filter sensitive fields before serialization too.

Values JSON.stringify changes or drops

  • undefined, functions, and symbols are omitted from objects.
  • Inside arrays, undefined, functions, and symbols become null.
  • Date objects serialize to ISO strings through toJSON().
  • NaN, Infinity, and -Infinity become null.
  • BigInt throws unless you convert it to a string or number first.
JSON.stringify({
  missing: undefined,
  fn: () => true,
  badNumber: NaN,
  createdAt: new Date('2024-01-15T10:30:00Z')
}, null, 2)

// {
//   "badNumber": null,
//   "createdAt": "2024-01-15T10:30:00.000Z"
// }

Circular references throw an error

JSON is a tree format. It cannot represent objects that point back to themselves.

const user = { name: 'Alice' }
user.self = user

JSON.stringify(user)
// TypeError: Converting circular structure to JSON

For logs, strip circular fields or use a purpose-built safe stringifier. For data exchange, redesign the payload so references are represented by IDs.

Format and validate JSON output

Paste stringified output into Jsonic's JSON Formatter to pretty print, validate, or minify it. Everything runs in your browser.

Open JSON Formatter