Introduction to JavaScript JSON stringify() Method
The JavaScript JSON stringify() method is an inbuilt method that converts a JavaScript value to a JSON string. It is used to convert a JavaScript object or value to a JSON string. It is commonly used when data is sent from a server to the client and vice versa. JSON (JavaScript Object Notation) is a lightweight data-interchange format that uses human-readable text to store and transmit data objects. It is easy for humans to read and write, and it is easy for machines to parse and generate.
Syntax
The syntax of the JavaScript JSON stringify() method is given below:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
JSON.stringify(value, replacer, space);
[/dm_code_snippet]
The JSON.stringify() method takes three parameters:
- value – The value which needs to be converted to a JSON string.
- replacer – It is an optional parameter which is used to transform values before they are stringified.
- space – It is an optional parameter which is used to add white space to the output string for readability.
Example of JSON stringify() Method
Let’s take an example of the JavaScript JSON stringify() method.
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
// JavaScript code
let person = {
name: "John",
age: 25
};
let jsonString = JSON.stringify(person);
console.log(jsonString);
[/dm_code_snippet]
The output of the above code is given below:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
{"name":"John","age":25}
[/dm_code_snippet]
You can also use the replacer parameter to modify the JSON output. For example:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
// JavaScript code
let person = {
name: "John",
age: 25
};
// Replacer function
function replacer(key, value) {
if (typeof value === "string") {
return undefined;
}
return value;
}
let jsonString = JSON.stringify(person, replacer);
console.log(jsonString);
[/dm_code_snippet]
The output of the above code is given below:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
{"age":25}
[/dm_code_snippet]
You can also use the space parameter to add white spaces to the output string for readability. For example:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
// JavaScript code
let person = {
name: "John",
age: 25
};
let jsonString = JSON.stringify(person, null, 4);
console.log(jsonString);
[/dm_code_snippet]
The output of the above code is given below:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
{ "name": "John", "age": 25 }
[/dm_code_snippet]
Conclusion
The JavaScript JSON stringify() method is an inbuilt method that converts a JavaScript value to a JSON string. It is used to convert a JavaScript object or value to a JSON string. It takes three parameters, the value which needs to be converted to a JSON string, the replacer parameter which is used to transform values before they are stringified and the space parameter which is used to add white space to the output string for readability.