JSON.stringify() is one of the most important methods in JavaScript for working with JSON data. It converts JavaScript objects, arrays, and values into JSON strings, making them ready for transmission, storage, or file output.
In this comprehensive guide, we'll cover everything you need to know about JSON.stringify(), from basic usage to advanced techniques. Use our free JSON.stringify() Online tool to test examples instantly.
Syntax
JSON.stringify(value, replacer, space)value (required)
The JavaScript object, array, or value to convert to a JSON string
replacer (optional)
A function or array to transform or filter values before stringifying
space (optional)
Number of spaces (0-10) for indentation, or a string for custom indentation
Interactive Examples
Basic Object Stringify
const obj = { name: "John", age: 30, city: "New York" };
const jsonString = JSON.stringify(obj);
console.log(jsonString);
// Output: '{"name":"John","age":30,"city":"New York"}'Pretty Print with Spaces
const obj = { name: "John", age: 30 };
const jsonString = JSON.stringify(obj, null, 2);
console.log(jsonString);
// Output:
// {
// "name": "John",
// "age": 30
// }Stringify Array
const arr = [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }];
const jsonString = JSON.stringify(arr);
console.log(jsonString);
// Output: '[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]'Using Replacer Function
const obj = { name: "John", age: 30, password: "secret123" };
const jsonString = JSON.stringify(obj, (key, value) => {
if (key === 'password') return undefined; // Exclude password
return value;
});
console.log(jsonString);
// Output: '{"name":"John","age":30}'Common Use Cases
1. Sending Data to API
fetch('/api/users', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'John', age: 30 })
});2. Storing in localStorage
const user = { name: 'John', preferences: { theme: 'dark' } };
localStorage.setItem('user', JSON.stringify(user));3. Creating JSON Files
const data = { users: [{ id: 1, name: 'Alice' }] };
const jsonContent = JSON.stringify(data, null, 2);
// Save to file or downloadWhat Cannot Be Stringified?
Some values are automatically handled or excluded:
- Functions: Omitted from output
- undefined: Omitted from objects, converted to null in arrays
- Symbols: Omitted from objects, converted to null in arrays
- Circular references: Throws TypeError
- Date objects: Converted to ISO string
JSON.stringify() Without Newlines
By default, JSON.stringify() creates compact JSON without newlines. To remove newlines from pretty-printed JSON, set the space parameter to 0 or omit it:
Compact Output (No Newlines)
const obj = { name: "John", age: 30 };
const compact = JSON.stringify(obj);
// Output: '{"name":"John","age":30}'
// Or explicitly:
const noNewlines = JSON.stringify(obj, null, 0);Use our JSON.stringify() online tool to test with different spacing values (0 for no newlines, 2+ for pretty print).
Best Practices
Use Pretty Print for Debugging
Use JSON.stringify(obj, null, 2) to make JSON readable during development
Filter Sensitive Data
Use replacer function to exclude passwords, tokens, or sensitive information
Handle Errors
Wrap in try-catch for circular references or large objects that might fail
Try JSON.stringify() Online
Use our free online tool to test JSON.stringify() with any JavaScript object. Supports pretty printing, replacer functions, and instant conversion.