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

03 Aug, 2021
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.
<!DOCTYPE html><html>  <head>    <title>How to Convert JS Object      to JSON String?</title>    <script src="https://code.jquery.com/jquery-1.12.4.min.js">  </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>
Output:
Example 2: JavaScript object is converted into a string and generate the alert message.
<!DOCTYPE html><html>  <head>    <title>How to Convert JS Object       to JSON String?</title>    <script src="https://code.jquery.com/jquery-1.12.4.min.js">  </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>
Output:Before Click on the button:After click on the button:
jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it’s philosophy of “Write less, do more”.You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.
javascript-object
JSON
JavaScript
JQuery
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Convert a string to an integer in JavaScript
How to calculate the number of days between two dates in javascript?
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
File uploading in React.js
JQuery | Set the value of an input text field
How to change selected value of a drop-down list using jQuery?
Form validation using jQuery
How to change the background color after clicking the button in JavaScript ?
How to Dynamically Add/Remove Table Rows using jQuery ?