Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

JSON.stringify

Converts a value to a JSON string.

Available since: ES5

Syntax

JSON.stringify(value, replacer, space)

Parameters

ParameterTypeDescription
valueanyThe value to convert: an object, an array, a string, a number.
replacerFunction | ArrayOptional. Selects or alters properties. Usually `null` is passed.
spacenumberOptional. The number of spaces for indentation — makes the result readable.

Return value

stringA JSON string.

Examples

An object becomes a string:

console.log(JSON.stringify({ name: 'Anna', tags: ['js', 'php'] }));

Output:

{"name":"Anna","tags":["js","php"]}

The third argument adds indentation:

console.log(JSON.stringify({ name: 'Anna', age: 25 }, null, 2));

Output:

{
  "name": "Anna",
  "age": 25
}

Notes

Properties whose value is undefined, and functions, are left out of the result.

See also

Related recipes

In the tutorial

  • JSON — The JSON format in JavaScript: turning objects into a string with JSON.stringify and back with JSON.parse.

Thanks for reading!

Donate to the site — we work hard for you.