top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to convert JSON text into object in Javascript?

+1 vote
587 views
How to convert JSON text into object in Javascript?
posted Jul 24, 2017 by Shyam Chakraborty

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
JSON.parse(jsonString);

1 Answer

0 votes

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().

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

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);


Converting JSON Text into Javascript Object


JSON Object :


Use of Javascript object :


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;


answer Jun 18, 2019 by Rushabh Verma R.
Similar Questions
+1 vote

Write a function which can take a random JSON and transform the data into multiple line human readable title value pairs.

For example:

Given JSON

{
        university: 'Oxford',
        batch: '2019-2020',
        address: {
            street: '144 Main',
            city: 'London',
            country: 'UK',
            contact: {
                fax: 'XXXXXX',
                phone: 'YYYYY'
            }
        },
        students: [{
            name: 'Jon Doe',
            age: '22'
        },{
            name: 'Mike Wilson',
            age: '32'
        },{
            name: 'David',
            age: '28'
        }]
}

Output:

university: Oxford
batch: 2019-2020
address street: 144 Main
address city: London
address country: UK
address contact fax: XXXXXX
address contact phone: YYYYY
students 1 name: Jon Doe
students 1 age: 22
students 2 name: Mike Wilson
students 2 age: 32
students 3 name: David
students 3 name: 28
+2 votes

I am Getting may JSON array . But i can't know how to fill dropdown list with this array ...

<script>
  function Customer_Id()  
  {  
      var Cus_id = document.getElementById("customer").value;  
      alert(Cus_id);
      var xhr;  
      if (window.XMLHttpRequest) // Mozilla, Safari, ... 
       {   
            xhr = new XMLHttpRequest();  
       }  
        else if (window.ActiveXObject) // IE 8 and older 
        {   
            xhr = new ActiveXObject("Microsoft.XMLHTTP");  
        }  
          var data = "Customer_ID=" + Cus_id;  

             xhr.open("POST", "Volumne_WebUser.php", true);   
             xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                    
             xhr.send(data);
              xhr.onreadystatechange = display_data;  
      function display_data() {  
     if (xhr.readyState == 4) {  
      if (xhr.status == 200) {  
      var response = xhr.responseText;
      var obj = JSON.parse(response);
      alert(obj['Name']);     
      } else {  
        alert('There was a problem with the request.');          
      }  
     }  
    }  
  }
</script>
...