Converting JSON text to JavaScript Object

Pre-requisite: JavaScript JSON
JSON (JavaScript Object Notation) is a lightweight data-interchange format. As its name suggests, JSON is derived from the JavaScript programming language, but it’s available for use by many languages including Python, Ruby, PHP, and Java and hence, it can be said as language-independent. For humans, it is easy to read and write and for machines, it is easy to parse and generate. It is very useful for storing and exchanging data.

A JSON object is a key-value data format that is typically rendered in curly braces. JSON object consist of curly braces ( { } ) at the either ends and have key-value pairs inside the braces. Each key-value pair inside braces are separated by comma (, ). JSON object looks something like this :

{
    "key":"value",
    "key":"value",
    "key":"value",
}

Example for a JSON object :

{
    "rollno":101",
    "name":"Mayank",
    "age":20,
}

Conversion of JSON text to Javascript Object

JSON text/object can be converted into Javascript object using the function JSON.parse().

filter_none

edit
close

play_arrow

link
brightness_4
code

var object1 = JSON.parse('{"rollno":101, "name":"Mayank", "age":20}');

chevron_right


For getting the value of any key from a Javascript object, we can use the values as: object1.rollno



If we pass a invalid JSON text to the function JSON.parse(), it will generate error (no output is displayed when using in tag of HTML).

Examples : Here, in example, the JSON text ‘jsonobj’ have 3 key-value pair. Each of the pairs were able to be accessed by the Javascript object ‘obj’ using dot ( . ). ‘obj’ was a javascript object which was the result of the function JSON.parse().

var jsonobj = ‘{ “name”:”Brendan Eich”, “designerof”:”Javascript”, “bornin”:”1961″ }’;
var obj = JSON.parse(jsonobj);
print(“JSON Object/Text : “);
print(obj.name + “, who was born in ” + obj.bornin + “, was the designer of ” + obj.designerof);
print(“Use of Javascript object : “);
print(jsonobj);

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
<body>
  
<h2>Converting JSON Text into Javascript Object</h2>
<b>JSON Object :</b>
<p id="demo"></p>
<b>Use of Javascript object :</b>
<p id="demo1"></p>
  
<script>
var jsonobj ='{ "name":"Brendan Eich","designerof":"Javascript","bornin":"1961" }';
  
// Here we convert JSON to object
var obj = JSON.parse(jsonobj);
  
document.getElementById("demo1").innerHTML = 
                     obj.name + ", who was born in "
                     + obj.bornin + ", was the designer of "
                     + obj.designerof;
document.getElementById("demo").innerHTML =jsonobj;
</script>
</body>
</html>

chevron_right




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.