JSON.stringify()
Converts a JavaScript value (object or array) to a JSON string.
JSON.stringify(value, [replacer], [space])This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Converts a JavaScript value (object or array) to a JSON string.
The `JSON.stringify()` method is a static function that converts a JavaScript value (typically an object or array) into a JSON formatted string. This is essential for sending data to a web server, storing data in local storage, or writing data to a file, as JSON is a common data interchange format. The method handles primitive values (strings, numbers, booleans, `null`), objects, and arrays. It ignores functions, `undefined`, and `Symbol` values. If these are found in an object, the property will be omitted; if found in an array, they will be replaced by `null`. The optional `replacer` argument can be a function or an array. As a function, it filters and transforms values before stringification. As an array, it specifies which properties of the object should be included. The optional `space` argument can be a string or number to insert whitespace into the output JSON string for readability (e.g., `2` for 2-space indentation). The time complexity is generally O(n) where n is the size of the object being stringified, as it needs to traverse the object structure.
Quick reference
Syntax
JSON.stringify(value, [replacer], [space])
Inputs
Parameters
See it in practice
Examples
Stringifying a simple object
const user = { name: "Bob", age: 25, isAdmin: false };
const jsonString = JSON.stringify(user);
console.log(jsonString);{"name":"Bob","age":25,"isAdmin":false}
The JavaScript object is converted into a compact JSON string.
Stringifying with indentation
const data = { id: 1, items: ["apple", "banana"] };
const prettyJson = JSON.stringify(data, null, 2);
console.log(prettyJson);{ "id": 1, "items": [ "apple", "banana" ] }
The `space` argument (2) adds 2-space indentation for better readability. `null` as the replacer means no transformation.
Using a replacer array to select properties
const product = { name: "Laptop", price: 1200, category: "Electronics", stock: 50 };
const selectedProps = JSON.stringify(product, ["name", "price"]);
console.log(selectedProps);{"name":"Laptop","price":1200}
Only the "name" and "price" properties are included in the resulting JSON string.
Debug faster
Common Errors
Circular references
Cause: Attempting to stringify an object that contains circular references (an object referencing itself directly or indirectly), which leads to a `TypeError`.
Fix: Ensure your objects do not have circular references. If they must, manually handle them in a `replacer` function or use a library that can detect and manage them (e.g., by replacing them with a placeholder).
const obj = {};
obj.self = obj; // Circular reference
try {
JSON.stringify(obj);
} catch (e) {
console.error(e.name + ": " + e.message);
}Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Converts a JavaScript value (object or array) to a JSON string.
value: The JavaScript value to convert to a JSON string. replacer: An optional function to transform values, or an array of property names to include. space: An optional string or number of spaces to use for indentation.
Circular references: Ensure your objects do not have circular references. If they must, manually handle them in a `replacer` function or use a library that can detect and manage them (e.g., by replacing them with a placeholder).