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

Abhi73
Tera Expert

Response from REST API for a custom application in ServiceNow. As it is a JSON Array, JSON.parse() is not working in javascript. I need to parse the below response, fetch number from the response & display it on an external html page

{
  "result": [
    {
      "parent": "",
      "made_sla": "true",
      "watch_list": "",
      "u_when_needed": "2017-05-18 21:52:02",
      "upon_reject": "cancel",
      "sys_updated_on": "2017-01-30 22:51:35",
      "u_what_needed": "legal1",
      "approval_history": "",
      "number": "NI002004",
      "sys_updated_by": "admin",
      "opened_by": {
        "link": "https://dev22387.service-now.com/api/now/table/sys_user/6816f79cc0a8016401c5a33be04be441",
        "value": "6816f79cc0a8016401c5a33be04be441"
      },
      "user_input": "",
      "sys_created_on": "2017-01-30 22:51:35",
      "sys_domain": {
        "link": "https://dev22387.service-now.com/api/now/table/sys_user_group/global",
        "value": "global"
      },
      "state": "3",
      "sys_created_by": "admin",
      "knowledge": "false",
      "order": "",
      "closed_at": "2017-01-30 22:51:35",
      "cmdb_ci": "",
      "delivery_plan": "",
      "impact": "3",
      "u_requested_for": {
        "link": "https://dev22387.service-now.com/api/now/table/sys_user/46d44a23a9fe19810012d100cca80666",
        "value": "46d44a23a9fe19810012d100cca80666"
      },
      "active": "false",
      "work_notes_list": "",
      "business_service": "",
      "priority": "4",
      "sys_domain_path": "/",
      "time_worked": "",
      "expected_start": "",
      "opened_at": "2017-01-30 22:50:27",
      "business_duration": "",
      "group_list": "",
      "work_end": "",
      "approval_set": "",
      "work_notes": "",
      "short_description": "When does the ESPP window close?",
      "correlation_display": "",
      "delivery_task": "",
      "work_start": "",
      "assignment_group": "",
      "new_field_1": "",
      "additional_assignee_list": "",
      "description": "",
      "calendar_duration": "",
      "close_notes": "",
      "sys_class_name": "x_58872_needit_needit",
      "closed_by": {
        "link": "https://dev22387.service-now.com/api/now/table/sys_user/6816f79cc0a8016401c5a33be04be441",
        "value": "6816f79cc0a8016401c5a33be04be441"
      },
      "follow_up": "",
      "sys_id": "e1bc2a030fe03200cd674f8ce1050ea4",
      "contact_type": "",
      "urgency": "3",
      "company": "",
      "reassignment_count": "0",
      "activity_due": "",
      "assigned_to": "",
      "u_request_type": "legal",
      "comments": "",
      "approval": "not requested",
      "sla_due": "",
      "comments_and_work_notes": "",
      "due_date": "",
      "sys_mod_count": "0",
      "sys_tags": "",
      "escalation": "0",
      "upon_approval": "proceed",
      "correlation_id": "",
      "location": ""
    }
  ]
}

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,


Finally i was able to get number, using below script:

 

var obj = {"result":[{"parent":"","made_sla":"true","watch_list":"","u_when_needed":"2017-05-18 21:52:02","upon_reject":"cancel","sys_updated_on":"2017-01-30 22:51:35","u_what_needed":"legal1","approval_history":"","number":"NI002004","sys_updated_by":"admin","opened_by":{"link":"https://dev22387.service-now.com/api/now/table/sys_user/6816f79cc0a8016401c5a33be04be441","value":"6816f79cc0a8016401c5a33be04be441"},"user_input":"","sys_created_on":"2017-01-30 22:51:35","sys_domain":{"link":"https://dev22387.service-now.com/api/now/table/sys_user_group/global","value":"global"},"state":"3","sys_created_by":"admin","knowledge":"false","order":"","closed_at":"2017-01-30 22:51:35","cmdb_ci":"","delivery_plan":"","impact":"3","u_requested_for":{"link":"https://dev22387.service-now.com/api/now/table/sys_user/46d44a23a9fe19810012d100cca80666","value":"46d44a23a9fe19810012d100cca80666"},"active":"false","work_notes_list":"","business_service":"","priority":"4","sys_domain_path":"/","time_worked":"","expected_start":"","opened_at":"2017-01-30 22:50:27","business_duration":"","group_list":"","work_end":"","approval_set":"","work_notes":"","short_description":"When does the ESPP window close?","correlation_display":"","delivery_task":"","work_start":"","assignment_group":"","new_field_1":"","additional_assignee_list":"","description":"","calendar_duration":"","close_notes":"","sys_class_name":"x_58872_needit_needit","closed_by":{"link":"https://dev22387.service-now.com/api/now/table/sys_user/6816f79cc0a8016401c5a33be04be441","value":"6816f79cc0a8016401c5a33be04be441"},"follow_up":"","sys_id":"e1bc2a030fe03200cd674f8ce1050ea4","contact_type":"","urgency":"3","company":"","reassignment_count":"0","activity_due":"","assigned_to":"","u_request_type":"legal","comments":"","approval":"not requested","sla_due":"","comments_and_work_notes":"","due_date":"","sys_mod_count":"0","sys_tags":"","escalation":"0","upon_approval":"proceed","correlation_id":"","location":""}]};

var a = JSON.stringify(obj);
var parser = new JSONParser();
var parsed = parser.parse(a);
var xyz = parsed.result[0].number;
gs.log(xyz);

 

Thanks,
Ashutosh Munot

 

Cheers: Mark Answer Helpful or Correct if Satisfied.

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

}