How to parse JSON object using JSON.stringify() in JavaScript ?
Last Updated :
26 Jul, 2022
In this article, we will see how to parse a JSON object using the JSON.stringify function. The JSON.stringify() function is used for parsing JSON objects or converting them to strings, in both JavaScript and jQuery . We only need to pass the object as an argument to JSON.stringify() function.
Syntax:
JSON.stringify(object, replacer, space);
Parameter Values: This function accepts 3 parameters that are described below:
object: It is the required value that is used to be parsed or converted to a string.
replacer: To filter the result, a function or an array is used. If replacer is null or not given, the resultant JSON string contains all of the object’s properties. This is an optional parameter.
space: This parameter controls the space in the final string created by the JSON.stringify() method. It can be either a number or a string. If this is a number, it denotes the number of white space characters to use for indenting; this value is limited to 10. If this is a string, the whole string or its first 10 characters is utilized as white space. No white space is used if this option is not given (null).
Return Value: A string representing the given value.
Example 1: In the below example, the JSON object is being passed as a value to the JSON.stringify() function to be parsed.
Javascript
<script>
var obj = {
name: "Vishal",
email: "abc@gmail.com",
};
var result = JSON.stringify(obj);
document.write("parsed object = " + result);
</script>
Output:
parsed object = {
"name":"Vishal",
"email":"abc@gmail.com"
}
Example 2: In the below example, the array is declared inside the object that is being passed as a value to the JSON.stringify() function to be parsed.
Javascript
<script>
var obj = {
company: "GeeksforGeeks",
courses: ['DSA', 'Web Tech',
'Placement_Preparation', 'DDA']
};
var result = JSON.stringify(obj);
document.write("parsed object = " + result);
</script>
Output:
parsed object = {
"company":"GeeksforGeeks",
"courses":["DSA","Web Tech","Placement_Preparation","DDA"]
}
Like Article
Suggest improvement
Share your thoughts in the comments
Add Your Comment
Please Login to comment...