JSON.stringify()

ES5+

Converts a JavaScript value to a JSON string.

Syntax

JSON.stringify(value, replacer, space)

Parameters

value any

The value to convert to a JSON string

replacer Function | Array optional

A function that alters the behavior of the stringification process, or an array of property names to include

space number | string optional

A string or number used to insert white space into the output JSON string for readability

Return Value

string

A JSON string representing the given value

Examples

JavaScript
const obj = { name: 'John', age: 30 };
console.log(JSON.stringify(obj));
Output:
// '{"name":"John","age":30}'

Related Methods