Skip to content

JavaScript JSON parse() Method | Example code

  • by

JavaScript JSON parse() method is used to parse a JSON string, constructing the JavaScript value or object described by the string.

JSON.parse(text)
JSON.parse(text, reviver)

Note: you will get a syntax error if the text is not in JSON format.

JavaScript JSON parse example

Simple example code use the JavaScript function JSON.parse() to convert text into a JavaScript object:

<!DOCTYPE html>
<html>
<body>

  <script>

    var data = '{"name":"John", "age":30, "city":"New York"}';

    var res= JSON.parse(data)

    console.log(res)
    
  </script>

</body>
</html>

Output:

JavaScript JSON parse Method

More examples Using JSON.parse()

JSON.parse('{}');              // {}
JSON.parse('true');            // true
JSON.parse('"foo"');           // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null');            // null

Do comment if you have any doubts or suggestions on this JS Json topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading