JSON.stringify(value, replacer, space)
- Converts a JavaScript object into a JSON string
value
: The object or value to convert
replacer
(optional): A function or array to filter properties
space
(optional): Indentation level for formatting
const obj = { name: "Alice", age: 25 };
console.log(JSON.stringify(obj));
// '{"name":"Alice","age":25}'
JSON.parse(jsonString, reviver)
- Converts a JSON string into a JavaScript object
jsonString
: The string to parse.
reviver
(optional): A function to transform the result.
const jsonString = '{"name":"Alice","age":25}';
const obj = JSON.parse(jsonString);
console.log(obj.name); // "Alice"