JSON Parse

Next ❯Objects

Used to convert a JSON string into an object

  • Well, Every data came from a server side is in string
    So to convert JSON string into JavaScript object we require JSON.parse()
  • Remember! You can also convert string like array via this method into an array

Syntax

JSON.parse(jsonString)

Example

var jsonString = '{"name":"Riya","age":23}';  // String
Convert into
var obj = {"name":"Riya","age":23};  //Object

var jsonString = '{"name":"Riya","age":23}';
var obj = JSON.parse(jsonString);
$("p").html("Before : "+jsonString);
$("p").append("<br>After Parse : "+ obj);
JSON.parsesubject
JSON.parseclose
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
<!--Required jQuery Library file-->
<script src="jquery.min.js"></script>
</head>
<body>
<p style="text-align:center;"></p>
<script>
var jsonString = '{"name":"Riya","age":23}';
var obj = JSON.parse(jsonString);
$("p").html("Before : "+jsonString);
$("p").append("<br>After Parse : "+ obj);
</script>
</body>
</html>

  • To convert your object back to string, we have JSON.stringify() method, you will see it in coming section
  • To access object data , see next section

  • Objects
❮ Prev Ways To Add
Next ❯Objects
TryOut Examples"Learn to Explore..!"

TryOut Editor

receipt