How to Convert JS Object to JSON String in JQuery/Javascript?

Given a JS Object and is the task to Convert it to JSON String.
Using JSON.stringify() method: The JSON.stringify() method in javascript allows us to take a JavaScript object or Array and create a JSON string out of it.

Syntax:

JSON.stringify(value, replacer, space)

Approach:



  • Store the JSON object into the variable.
  • Pass that variable in the JSON.stringify() as an argument.
  • It will return the value which is to be converted into a JSON string.

Example 1: JavaScript object is converted into a string.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>How to Convert JS Object
      to JSON String?</title>
  </script>
</head>
  
<body>
    <center>
        <h1 style="color:green;"
        GeeksForGeeks 
    </h1>
        <h3>How to Convert JS Object to JSON String?
    </h3>
        <h4>
        ----JSON Object----<br>
        {name: "Shubham", age: 21,
          Intern: "Geeksoforgeeks",
          Place:"Work from Home"}
    </h4> ----JSON String----
        <br>
        <script>
            // Sample JS object
            var geeks = {
                name: "Shubham",
                age: 21,
                Intern: "Geeksoforgeeks",
                Place: "Work from Home"
            };
  
            // Converting JS object to JSON string
            var gfg = JSON.stringify(geeks);
  
            document.write(gfg);
            /* Prints: {"name": "Shubham", "age": 21,
            "Intern": "Geeksoforgeeks",
            "Place":"Work from Home"}*/
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:

Example 2: JavaScript object is converted into a string and generate the alert message.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>How to Convert JS Object 
      to JSON String?</title>
  </script>
</head>
  
<body>
    <center>
        <h1 style="color:green;"
        GeeksForGeeks 
    </h1>
        <h3>How to Convert JS Object to JSON String?
    </h3>
        <h4>
        ----JSON Object----<br>
        {name: "Shubham", age: 21,
          Intern: "Geeksoforgeeks",
          Place:"Work from Home"}
    </h4>
        <button onclick="myFunction()">Click</button>
        <script>
            function myFunction() {
                // Sample JS object
                var geeks = {
                    name: "Shubham",
                    age: 21,
                    Intern: "Geeksoforgeeks",
                    Place: "Work from Home"
                };
  
                // Converting JS object to JSON string
                var gfg = JSON.stringify(geeks);
                var print = "----JSON String----";
                alert(print + "\n" + gfg);
                /* alert: {"name": "Shubham", "age": 21,
                "Intern": "Geeksoforgeeks",
                "Place":"Work from Home"}*/
            }
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:
Before Click on the button:

After click on the button:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.