The below example show how we can use JSON data in JavaScript Application.
Example
Suppose we have a web API and it is uploaded on
web server. So we received this text from a web server:
'{"name":"Rohatash", "age":36, "Email":"test@gmail.com"}'
Use the JavaScript function JSON.parse() to convert text into a JavaScript object:
const parseobj = JSON.parse('{{"name":"Rohatash", "age":36, "Email":"test@gmail.com"}}');
The below example you can use on HTML page
<html>
<body>
<h2>JavaScript Object from a JSON String</h2>
<p id="parsetestid"></p>
<script>
const jsontxt = '{"name":"Rohatash", "age":36, "Email":"test@gmail.com"}'
const obj = JSON.parse(jsontxt);
document.getElementById("parsetestid").innerHTML = obj.name + ", " + obj.age + ", " + obj.Email;
</script>
</body>
</html>
Now run the code on browser. The following output will show:
Output