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:
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