JSON.stringify() Online - Convert Object to String
Convert JavaScript objects to JSON strings. Test JSON.stringify() with custom spacing, replacer functions, and see the serialized output.
Size:
14
Paste your JSON here to see stringify behavior...
OutputEMPTY
Formatted output will appear here...
How JSON.stringify() Works
// JavaScript object (what you have)
const user = {
name: "John",
age: 30,
active: true
};
// Stringify to JSON (what you get)
const jsonString = JSON.stringify(user);
// '{"name":"John","age":30,"active":true}'
// With pretty printing
const pretty = JSON.stringify(user, null, 2);
// {
// "name": "John",
// "age": 30,
// "active": true
// }JSON.stringify() Parameters
value
The JavaScript value to convert. Can be any type except Symbol.
JSON.stringify(myObject)
replacer
Optional. Array of keys to include, or a function to transform values.
JSON.stringify(obj, ['name', 'age'])
space
Optional. Number of spaces for indentation (0-10), or a string.
JSON.stringify(obj, null, 2)
Common JSON.stringify() Use Cases
Sending Data to API
fetch('/api/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: 'John',
email: '[email protected]'
})
});Saving to localStorage
const settings = {
theme: 'dark',
fontSize: 16
};
// Save to localStorage
localStorage.setItem(
'settings',
JSON.stringify(settings)
);Filtering Keys with Replacer
const user = {
name: 'John',
password: 'secret',
email: '[email protected]'
};
// Only include specific keys
JSON.stringify(user, ['name', 'email']);
// {"name":"John","email":"[email protected]"}Pretty Print for Debugging
const data = { a: 1, b: { c: 2 } };
// Minified (default)
JSON.stringify(data);
// {"a":1,"b":{"c":2}}
// Pretty printed
JSON.stringify(data, null, 2);
// {
// "a": 1,
// "b": {
// "c": 2
// }
// }Values That Cannot Be Stringified
| Value Type | In Object | In Array |
|---|---|---|
| undefined | Omitted (key removed) | Converted to null |
| function | Omitted (key removed) | Converted to null |
| Symbol | Omitted (key removed) | Converted to null |
| BigInt | Throws TypeError | |
| Circular reference | Throws TypeError | |
| Infinity, NaN | Converted to null | |
Frequently Asked Questions
How do I pretty print JSON with JSON.stringify()?
Use the third parameter (space) to add indentation:
JSON.stringify(obj, null, 2) for 2-space indent, or JSON.stringify(obj, null, 4) for 4-space indent. You can also use a string like "\t" for tabs. How do I exclude certain keys from JSON.stringify()?
Use the replacer parameter. To include only specific keys:
JSON.stringify(obj, ['name', 'email']). To exclude keys, use a function: JSON.stringify(obj, (key, val) => key === 'password' ? undefined : val)Why does JSON.stringify() return undefined?
JSON.stringify() returns undefined only when called with a pure function, undefined, or Symbol value at the top level (not inside an object). For example:
JSON.stringify(undefined) returns undefined, not the string "undefined". How do I handle circular references?
JSON.stringify() throws a TypeError for circular references. Use a replacer function to detect and handle them, or use a library like "circular-json" or "flatted" that supports circular structures.
How do I stringify Date objects?
Date objects are automatically converted to ISO 8601 strings by JSON.stringify(). For example,
new Date() becomes "2026-01-29T18:30:00.000Z". Use JSON.parse() with a reviver to convert back to Date.