JSON

JSON.stringify

Converts a JavaScript value to a JSON string, optionally replacing values or including only specified properties

Syntax

JavaScript
JSON.stringify(value, replacer?, space?)

Parameters

ParameterTypeDescription
valueanyThe value to convert to a JSON string
replacerFunction | ArrayAlters the stringification process
spacestring | numberAdds indentation, white space, and line breaks for readability

Return Value

A JSON string representing the given value, or undefined

Examples

Basic Usage
const obj = { name: 'Alice', age: 30 };
console.log(JSON.stringify(obj));
// '{"name":"Alice","age":30}'
Practical Example
const obj = { name: 'Alice', age: 30, password: 'secret' };
const json = JSON.stringify(obj, ['name', 'age']);
console.log(json); // '{"name":"Alice","age":30}'
Advanced Usage
const data = { a: 1, b: { c: 2 } };
console.log(JSON.stringify(data, null, 2));
// {
//   "a": 1,
//   "b": {
//     "c": 2
//   }
// }

Understanding JSON.stringify

The JSON.stringify method in JavaScript converts a JavaScript value to a JSON string, optionally replacing values or including only specified properties. It belongs to the JSON object and is one of the most widely used methods for working with json values in modern JavaScript and TypeScript applications.

The method signature is JSON.stringify(value, replacer?, space?). It accepts 3 parameters: value, replacer, space. When called, it returns a json string representing the given value, or undefined. Understanding when and how to use stringify() helps you write more expressive, readable code.

Common use cases for JSON.stringify include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like json-parse, json-rawjson, date-tojson, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for JSON.stringify is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More JSON Methods

Other methods in the JSON object

Related Tools

More JSON Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.