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
Example
#const student = { name: 'Emma', level: 7, courses: ['HTML', 'CSS'], premium: false }
const json = JSON.stringify(student)
console.log(json)
console.log(typeof json)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
#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. Passnullto skip it.space
optional, number or string
Indentation for readable formatting, e.g.2means two spaces.
More examples
#const settings = { theme: 'dark', sound: true }
console.log(JSON.stringify(settings, null, 2))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))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)))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
MaporSet, 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. UsestructuredClone()instead.
Related entries
#- JSON.parse()Converts a string in JSON format into a JavaScript value, such as an object or array.
- structuredClone()Creates a deep copy of a value, including nested objects, dates, Maps, and Sets.
- fetch()Sends an HTTP request and returns a promise with the server’s response (a Response object).
- localStorageBrowser storage for key-value pairs that survives page refreshes and browser restarts.
- MapA collection of key-value pairs where the key can be any value and insertion order is preserved.
Spotted a mistake or missing an example? Write to us.