JavaScript JSON parse() Method

07 Jul, 2020
Below is the example of the JSON parse() Method.
Example:<script>        var obj = JSON.parse('{"var1":"Geeks",                                "var2":"forGeeks!"}');                  document.write(obj.var1 + "" + obj.var2);</script>                    
<script>        var obj = JSON.parse('{"var1":"Geeks",                                "var2":"forGeeks!"}');                  document.write(obj.var1 + "" + obj.var2);</script>                    
Output:GeeksforGeeks!
GeeksforGeeks!
The JSON.parse() method in JavaScript is used to parse a JSON string which is written in a JSON format and return a JavaScript object.
Syntax:
JSON.parse( string, function )
Parameters: This method accepts two parameter as mentioned above and described below:
string: It is required parameter and it contains string which is written in JSON format.
function: It is optional parameter and used to transform result. The function called for each item.
More example codes for the above method are as follows:
Example 1: This example parse a string and return the JavaScript object.
<!DOCTYPE html><html>  <head>    <title>        JavaScript JSON parse() Method    </title></head>  <body>          <h1>GeeksforGeeks</h1>          <h2>        JavaScript JSON parse() Method    </h2>          <p id="GFG"></p>          <!-- Script to parse a string and return        JavaScript object -->    <script>        var obj = JSON.parse('{"var1":"Hello",                                "var2":"Geeks!"}');                  document.getElementById("GFG").innerHTML                = obj.var1 + " " + obj.var2;    </script></body>  </html>                    
Output:
Example 2: This example use reviver function to parse a string and return the JavaScript object.
<!DOCTYPE html><html>  <head>    <title>        JavaScript JSON parse() Method    </title></head>  <body>          <h1>GeeksforGeeks</h1>          <h2>        JavaScript JSON parse() Method    </h2>          <p id="GFG"></p>          <!-- Script to parse a string and return        JavaScript object -->    <script>        var text = '{ "var1":"Amanda", "gender":"male"}';                  var obj = JSON.parse(text, function (key, value) {            if (value == "male") {                return ("female");            } else {                return value;            }        });        document.getElementById("GFG").innerHTML                = obj.var1 + ", " + obj.gender;     </script></body>  </html>                    
Output:
Supported browsers:
Chrome 4.0
Firefox 3.5
Opera 11.0
Internet Explorer 8.0
Safari 4.0
JavaScript-Methods
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Difference Between PUT and PATCH Request
How to Open URL in New Tab using JavaScript ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?