JSON.stringify()

Converts a JavaScript value, such as an object or array, into a string in JSON format.

Returns
A string in JSON format (or undefined, if you pass, say, just a function).
Changes the original
No
On this page

Example

#
JavaScript
const student = { name: 'Emma', level: 7, courses: ['HTML', 'CSS'], premium: false }

const json = JSON.stringify(student)
console.log(json)
console.log(typeof json)
Console
{"name":"Emma","level":7,"courses":["HTML","CSS"],"premium":false}
string

Definition and usage

#

The JSON.stringify() method turns an object or array into text in JSON format. In that form, the data can be sent to a server with fetch(), saved in localStorage, or written to a file. JSON.parse() performs the reverse operation.

Not everything can be written to JSON. Properties with the value undefined, functions, and symbols are skipped (and turned into null inside arrays), NaN and Infinity become null, and dates turn into strings in ISO format. Map and Set produce an empty object {}, and an object that references itself causes a TypeError.

The second argument (replacer) lets you select or transform the fields being written: an array of names keeps only those keys, and a function can change or skip any value. The third argument (space) adds indentation, making the result readable for humans.

Syntax

#
Syntax
JSON.stringify(value)
JSON.stringify(value, replacer)
JSON.stringify(value, replacer, space)

Parameters

#
  • value

    any value

    The data to convert to JSON, usually an object or array.
  • replacer

    optional, function or array

    A (key, value) function that transforms values, or an array of key names to keep. Pass null to skip it.
  • space

    optional, number or string

    Indentation for readable formatting, e.g. 2 means two spaces.

More examples

#
Readable formatting
JavaScript
const settings = { theme: 'dark', sound: true }
console.log(JSON.stringify(settings, null, 2))
Console
{
"theme": "dark",
"sound": true
}
What gets lost when converting to JSON
JavaScript
const quiz = {
  title: 'Loops quiz',
  deadline: new Date(Date.UTC(2026, 8, 28)),
  onFinish() {},
  bonus: undefined,
  score: NaN,
  tags: new Set(['js'])
}

console.log(JSON.stringify(quiz))
Console
{"title":"Loops quiz","deadline":"2026-09-28T00:00:00.000Z","score":null,"tags":{}}
Selecting fields with a replacer
JavaScript
const user = { id: 7, name: 'Mia', password: 'secret123', email: 'mia@example.com' }

console.log(JSON.stringify(user, ['id', 'name']))
console.log(JSON.stringify(user, (key, value) => (key === 'password' ? undefined : value)))
Console
{"id":7,"name":"Mia"}
{"id":7,"name":"Mia","email":"mia@example.com"}

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Before saving a Map or Set, convert it to a plain structure: Object.fromEntries(map) or [...set].
  • To preview large objects, use JSON.stringify(obj, null, 2). You’ll see the whole structure with indentation.
  • JSON.parse(JSON.stringify(obj)) is an old trick for a deep copy, but it loses dates, undefined, and functions. Use structuredClone() instead.

Related entries

#

Spotted a mistake or missing an example? Write to us.