top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Print human readable JSON to text in javascript?

+1 vote
584 views

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
posted Sep 13, 2020 by Atindra Kumar Nath

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

Try this code might help you
using stacking method to read and fetch data

const printHumanReadableJsonText = (jasonObject, stack="") => {
    for(let key in jasonObject){            
        if(typeof jasonObject[key] === "object" && jasonObject[key] !== null){
            if(Array.isArray(jasonObject[key])){
                for (let i=0; i<jasonObject[key].length; i++) {
                    stack = key + " " + (i+1) + " ";
                    printHumanReadableJsonText(jasonObject[key][i],stack);
                }
                stack="";
            }else {
                stack=stack+key+" ";
                printHumanReadableJsonText(jasonObject[key],stack);
            }
        }else {
            console.log(stack+key+": "+jasonObject[key]);
        }   
    }
    stack="";
}
console.log(printHumanReadableJsonText(jsonObj));
answer Sep 26, 2020 by Tanay Hanuman Toor
Thanks it's works like a charm
Similar Questions
+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>
...