Thursday, September 4, 2025
HomeLanguagesJavascriptDeserializing a JSON into a JavaScript object

Deserializing a JSON into a JavaScript object

To Deserialize a JSON into a JavaScript object, here we will use a common method JSON.parse() method.

JavaScript Object Notation is used to exchange data to or from a web server or RESTFull API. The data received from a web server is always a string. In order to use that data you need to parse the data with JSON.parse() which will returns a JavaScript Object or Array of Objects.

Syntax: 
 

JSON.parse( string, function )

Example 1: 
 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
    Deserializing a JSON into a JavaScript object
        using JSON parse() Method
    </title>
</head>
 
<body>
<center>
    <h1 style="color: green;">neveropen</h1>
    <h3>Deserializing a JSON into a JavaScript object</h3>
    <p id="neveropen"></p>
  
     
    <!-- Script to parse a string and return
        JavaScript object -->
    <script>
        var obj = JSON.parse('{"var1":"Hello", "var2":"Geeks!"}');
         
        document.getElementById("neveropen").innerHTML
                = obj.var1 + " " + obj.var2;
    </script>
    </center>
</body>
 
</html>                    


Output: 
 

Example 2: Here we will use reviver function to parse a string and return the JavaScript object. 
 

html




<!DOCTYPE html>
<html>
 
<body>
    <center>
        <h1 style="color:green">neveropen</h1>
        <h3>Convert a string into a date object.</h3>
 
        <p id="neveropen"></p>
 
 
        <script>
            var text = '{"name":" Pankaj_Singh",\
                 "birth":"1996-12-14", "city":"Jhansi"}';
            var obj = JSON.parse(text);
            obj.birth = new Date(obj.birth);
            document.getElementById("neveropen").innerHTML =
                             obj.name + ", " + obj.birth;
        </script>
</center>
</body>
 
</html>                   


Output: 
 

 

  • Chrome 4.0
  • Firefox 3.5
  • Opera 11.0
  • Internet Explorer 8.0
  • Safari 4.0

 

RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS