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.
There are 3 variations for the syntax of JSON.stringify() static-method. They are:
JSON.stringify(value)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
value | required | The JavaScript value to convert to a JSON string. |
This static-method converts a JavaScript value to a JSON string.
Returns value of type String
.
JSON.stringify(value, replacer)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
value | required | The JavaScript value to convert to a JSON string. |
replacer | optional | A 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
.
JSON.stringify(value, replacer, space)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
value | required | The JavaScript value to convert to a JSON string. |
replacer | optional | A function that transforms the results or an array of properties to include in the JSON string. |
space | optional | A 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
.
In JavaScript, we can use the JSON.stringify()
method to convert a simple object to a JSON string.
For example,
obj
with a key-value pair.JSON.stringify()
method to convert obj
to a JSON string.jsonString
.jsonString
to the console using console.log()
method.const obj = { key: 'value' };
const jsonString = JSON.stringify(obj);
console.log(jsonString);
{"key":"value"}
In JavaScript, we can use the JSON.stringify()
method with a replacer function to transform the property values during stringification.
For example,
obj
with a key-value pair.replacer
that transforms the value of the key 'key'
to uppercase.JSON.stringify()
method with the replacer function to convert obj
to a JSON string.jsonString
.jsonString
to the console using console.log()
method.const obj = { key: 'value' };
const replacer = (key, value) => key === 'key' ? value.toUpperCase() : value;
const jsonString = JSON.stringify(obj, replacer);
console.log(jsonString);
{"key":"VALUE"}
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,
obj
with nested properties.JSON.stringify()
method with a space
parameter set to 2
to convert obj
to a JSON string with indentation.jsonString
.jsonString
to the console using console.log()
method.const obj = { key: 'value', nested: { innerKey: 'innerValue' } };
const jsonString = JSON.stringify(obj, null, 2);
console.log(jsonString);
{ "key": "value", "nested": { "innerKey": "innerValue" } }
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.