Skip to content
Related Articles

Related Articles

Improve Article
Deserializing a JSON into a JavaScript object
  • Last Updated : 25 Jun, 2019

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 Objeccts.


JSON.parse( string, function )

Example 1:

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


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

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


    Supported Browser

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :