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:
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
.
✐ 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,
- We define a JavaScript object
obj
with a key-value pair. - We use the
JSON.stringify()
method to convertobj
to a JSON string. - The JSON string is stored in the variable
jsonString
. - We log
jsonString
to the console usingconsole.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,
- We define a JavaScript object
obj
with a key-value pair. - We define a replacer function
replacer
that transforms the value of the key'key'
to uppercase. - We use the
JSON.stringify()
method with the replacer function to convertobj
to a JSON string. - The JSON string is stored in the variable
jsonString
. - We log
jsonString
to the console usingconsole.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,
- We define a JavaScript object
obj
with nested properties. - We use the
JSON.stringify()
method with aspace
parameter set to2
to convertobj
to a JSON string with indentation. - The formatted JSON string is stored in the variable
jsonString
. - We log
jsonString
to the console usingconsole.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.