JSON.stringify()
JSON.stringify() converts a JavaScript value to a JSON string. It accepts optional replacer and spacing arguments for filtering and formatting.
JSON.stringify() converts a JavaScript value to a JSON string. It accepts optional replacer and spacing arguments for filtering and formatting.
const user = { name: "Alice", age: 30, active: true };
JSON.stringify(user);
// {"name":"Alice","age":30,"active":true}
// Pretty-print with 2-space indent
JSON.stringify(user, null, 2);
// {
// "name": "Alice",
// "age": 30,
// "active": true
// }
// Filter: only include specific keys
JSON.stringify(user, ["name", "age"]);
// {"name":"Alice","age":30}
Functions, undefined, and Symbols are omitted by JSON.stringify — they are not valid JSON values.
More in JavaScript