Convert string to JSON JavaScript | Example code

  • by

JSON is used to exchange data to/from a web server in string format. Receiving data from a web server is always a string. Use JSON parse() method convert string to JSON JavaScript.

JSON.parse(string);

Example convert string to JSON JavaScript

Simple example code. Make sure the string is in JSON format.

<!DOCTYPE html>
<html>
<body>

  <script>
    var msg = '{"name":"Tim", "age":30, "city":"Bangalore"}'

    var jsonData = JSON.parse(msg);

    console.log(typeof(msg))
    console.log(jsonData);
    console.log(typeof(jsonData))
  </script>

</body>
</html>

Output:

Convert string to JSON JavaScript

Do comment if you have any doubts or suggestions on this JS string to 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

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.