Home → JSON Stringify Online
JSON.stringify converts a JavaScript object into a JSON string.
JSON.stringify converts a JavaScript object into a JSON string. This tool runs entirely in your browser — no data is ever sent to a server. Free to use, no account required.
JSON.stringify() is the built-in JavaScript method for converting values to JSON strings. Understanding its behavior helps avoid common serialization pitfalls.
Objects and arrays are converted to their JSON representation: keys become quoted strings, values maintain their type, and the structure is preserved. Numbers, strings, and booleans serialize directly to their JSON equivalents.
Undefined values, functions, and symbols are not valid JSON. In objects these properties are silently omitted. In arrays they become null. NaN and Infinity also become null in JSON output.
JSON.stringify accepts two optional parameters that give you fine-grained control over serialization output.
The space parameter controls indentation. Pass 2 or 4 for space-indented output, or "\t" for tabs. Without a space parameter, output is compact with no whitespace — ideal for network transmission.
A replacer function or array filters or transforms values during serialization. An array of key names acts as an allowlist. A function receives each key and value and can return a transformed value or undefined to omit the property.
JSON.stringify() converts a JavaScript value to a JSON string. Understanding all its options and edge cases prevents common serialization bugs.
// Basic usage
JSON.stringify(value)
JSON.stringify(value, replacer)
JSON.stringify(value, replacer, space)
// Examples
JSON.stringify({a:1,b:2}) // '{"a":1,"b":2}'
JSON.stringify({a:1}, null, 2) // pretty with 2-space indent
JSON.stringify({a:1}, ["a"]) // only include key "a": '{"a":1}'
JSON.stringify({a:1}, (k,v) => v === 1 ? undefined : v) // filter
// Replacer function
const replacer = (key, value) => {
if (key === "password") return undefined; // omit password
if (key === "date" && value instanceof Date) return value.toISOString();
return value;
};
JSON.stringify(user, replacer, 2);
| Value Type | stringify() Behavior |
|---|---|
| undefined | Omitted in objects, null in arrays |
| function | Omitted in objects, null in arrays |
| Symbol | Omitted in objects, null in arrays |
| NaN / Infinity | Converted to null |
| Date object | Converted to ISO string via .toJSON() |
| BigInt | Throws TypeError — needs custom replacer |
| Circular reference | Throws TypeError — needs serializer |
| toJSON() method | Custom serialization if defined on object |
// Circular reference workaround
function safeStringify(obj) {
const seen = new WeakSet();
return JSON.stringify(obj, (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) return "[Circular]";
seen.add(value);
}
return value;
}, 2);
}
Explore more tools: All JSON Tools | Validator | Pretty Print | JSON Diff