Open In App

How to parse JSON object using JSON.stringify() in JavaScript ?

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:

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.




<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.




<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"]
}

Article Tags :