JavaScript JSON.stringify()
Syntax & Examples

JSON.stringify() static-method

The JSON.stringify() method converts a JavaScript value to a JSON string, optionally including only specified properties or transforming property values using a replacer function. You can also control the spacing in the resulting JSON string.


Syntax of JSON.stringify()

There are 3 variations for the syntax of JSON.stringify() static-method. They are:

1.
JSON.stringify(value)

Parameters

ParameterOptional/RequiredDescription
valuerequiredThe JavaScript value to convert to a JSON string.

This static-method converts a JavaScript value to a JSON string.

Returns value of type String.

2.
JSON.stringify(value, replacer)

Parameters

ParameterOptional/RequiredDescription
valuerequiredThe JavaScript value to convert to a JSON string.
replaceroptionalA function that transforms the results or an array of properties to include in the JSON string.

This static-method converts a JavaScript value to a JSON string, with a replacer function to transform the properties.

Returns value of type String.

3.
JSON.stringify(value, replacer, space)

Parameters

ParameterOptional/RequiredDescription
valuerequiredThe JavaScript value to convert to a JSON string.
replaceroptionalA function that transforms the results or an array of properties to include in the JSON string.
spaceoptionalA string or number that's used to insert white space into the output JSON string for readability purposes.

This static-method converts a JavaScript value to a JSON string, with a replacer function to transform the properties and a specified space for indentation.

Returns value of type String.



✐ Examples

1 Using JSON.stringify() with a simple object

In JavaScript, we can use the JSON.stringify() method to convert a simple object to a JSON string.

For example,

  1. We define a JavaScript object obj with a key-value pair.
  2. We use the JSON.stringify() method to convert obj to a JSON string.
  3. The JSON string is stored in the variable jsonString.
  4. We log jsonString to the console using console.log() method.

JavaScript Program

const obj = { key: 'value' };
const jsonString = JSON.stringify(obj);
console.log(jsonString);

Output

{"key":"value"}

2 Using JSON.stringify() with a replacer function

In JavaScript, we can use the JSON.stringify() method with a replacer function to transform the property values during stringification.

For example,

  1. We define a JavaScript object obj with a key-value pair.
  2. We define a replacer function replacer that transforms the value of the key 'key' to uppercase.
  3. We use the JSON.stringify() method with the replacer function to convert obj to a JSON string.
  4. The JSON string is stored in the variable jsonString.
  5. We log jsonString to the console using console.log() method.

JavaScript Program

const obj = { key: 'value' };
const replacer = (key, value) => key === 'key' ? value.toUpperCase() : value;
const jsonString = JSON.stringify(obj, replacer);
console.log(jsonString);

Output

{"key":"VALUE"}

3 Using JSON.stringify() with space for indentation

In JavaScript, we can use the JSON.stringify() method with the space parameter to add white space to the resulting JSON string for readability.

For example,

  1. We define a JavaScript object obj with nested properties.
  2. We use the JSON.stringify() method with a space parameter set to 2 to convert obj to a JSON string with indentation.
  3. The formatted JSON string is stored in the variable jsonString.
  4. We log jsonString to the console using console.log() method.

JavaScript Program

const obj = { key: 'value', nested: { innerKey: 'innerValue' } };
const jsonString = JSON.stringify(obj, null, 2);
console.log(jsonString);

Output

{
  "key": "value",
  "nested": {
    "innerKey": "innerValue"
  }
}

Summary

In this JavaScript tutorial, we learned about stringify() static-method of JSON: the syntax and few working examples with output and detailed explanation for each example.