Parse JSON array & display value in a html page

Abhi73
Tera Expert

Hello experts,

 

I have created a HTML page & consumes custom application REST API. I need to parse the response which has JSON array & require to display the record id in the HTML page.  I have tried to use it as JSON object but it did not help. I have attached the sample response in the thread.

Valuable feedback is required.

 

1 ACCEPTED SOLUTION

Abhi73
Tera Expert

Thread closed. Issue has been fixed by the below code 

var resp=this.response;
respo = JSON.parse(resp);
//console.log(respo.result.length);
for (var i=0; i < respo.result.length; i++)
{
console.log(respo.result[i].number);
alert(respo.result[i].number);

}

View solution in original post

12 REPLIES 12

Already tried it but of no help.

 

rad2
Mega Sage

User JSONParser() to read the json string to object.

var json = new JSONParser();
 var data = json.parse(responseBody);
 var result = data["result"];

Do share the script if this does not help you.

This does not work.

Below is the JS to parse the response

 

function retrievelist()
{
var requestBody = "";

var client=new XMLHttpRequest();
client.open("get","https://dev22387.service-now.com/api/now/table/x_58872_needit_needit?sysparm_fields=number&sysparm_limit=2");

client.setRequestHeader('Accept','application/json');
client.setRequestHeader('Content-Type','application/json');

//Eg. UserName="admin", Password="admin" for this code sample.
client.setRequestHeader('Authorization', 'Basic '+btoa('admin'+':'+'******'));

client.onreadystatechange = function() {
if(this.readyState == this.DONE) {
//document.getElementById("response").innerHTML=this.status + this.response;
alert(this.response);
//var cc=this.response.toString();
// var json = new JSONParser();
var data = json.parse(this.response);
var result = data["result"];
alert(result);
}
};
client.send(requestBody);
}

Did you try by not commenting out the line

// var json = new JSONParser();

 

try JSON.parse(this.response) instead

 

I tried it without commenting it.

Also, I have tried the below earlier & it was not of much help

JSON.parse(this.response)