JSON.stringify
Converts a JavaScript value to a JSON string, optionally replacing values or including only specified properties
Syntax
JSON.stringify(value, replacer?, space?)Parameters
| Parameter | Type | Description |
|---|---|---|
| value | any | The value to convert to a JSON string |
| replacer | Function | Array | Alters the stringification process |
| space | string | number | Adds indentation, white space, and line breaks for readability |
Return Value
A JSON string representing the given value, or undefined
Examples
const obj = { name: 'Alice', age: 30 };
console.log(JSON.stringify(obj));
// '{"name":"Alice","age":30}'const obj = { name: 'Alice', age: 30, password: 'secret' };
const json = JSON.stringify(obj, ['name', 'age']);
console.log(json); // '{"name":"Alice","age":30}'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
JSON.parseParses a JSON string, constructing the JavaScript value or object described by the string
JSON.rawJSONCreates a raw JSON object that can be included in a JSON string without being re-serialized
Date.prototype.toJSONReturns a string representation of the Date object for use by JSON.stringify, which calls toISOString under the hood
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.