Parsing the JSON obj inIntegration

Madhan007
Tera Contributor

Hi, I got this Response Body from a API call I made to another ServiceNow Instance.
{"result":[{"calendar_integration":"1","country":"","user_password":"U3X0xj01Fr","last_login_time":"","source":"","sys_updated_on":"2023-12-11 12:27:28","building":"","web_service_access_only":"true","notification":"2","enable_multifactor_authn":"false","sys_updated_by":"system","sys_created_on":"2023-06-25 12:00:57","sys_domain":{"link":"https://dev180310.service-now.com/api/now/table/sys_user_group/global","value":"global"},"state":"","vip":"false","sys_created_by":"system","zip":"","home_phone":"","time_format":"","last_login":"","default_perspective":"","active":"true","sys_domain_path":"/","cost_center":"","phone":"","name":"Virtual Agent","employee_number":"","password_needs_reset":"false","gender":"","city":"","failed_attempts":"","user_name":"virtual.agent","roles":"","title":"","sys_class_name":"sys_user","sys_id":"4583e347b4672110320f8dc279c804bb","federated_id":"MApWBRP6bOMaUi1JwFN32jJhX72bItv8zP0QwSsOdUQ=","internal_integration_user":"false","ldap_server":"","mobile_phone":"","street":"","company":"","department":"","first_name":"Virtual","email":"","introduction":"","preferred_language":"","manager":"","locked_out":"false","sys_mod_count":"3","last_name":"Agent","photo":"","avatar":"","middle_name":"","sys_tags":"","time_zone":"","schedule":"","date_format":"","location":""}]}

But when I tried to parse this using JSON.parse() method, it  just returns as [object Object]. whats the issue here?


Code I Used to call Rest message from BR-

 var r = new sn_ws.RESTMessageV2('dev180310', 'Default GET');
 r.setStringParameterNoEscape('name', current.u_name);
 
//parsing the response
 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
 var responseOBJ = JSON.parse(responseBody);
 gs.info("Buddy ResponseBody"+responseBody);
 gs.info("Buddy Responseobj"+responseOBJ);  // this is being logged as  [object Object].
 gs.info("buddy status code"+httpStatus);
 //current.u_name = responseOBJ.name;
 gs.info("Buddy"+responseOBJ.result); gs.addInfoMessage("started");
 var r = new sn_ws.RESTMessageV2('dev180310', 'Default GET');
 r.setStringParameterNoEscape('name', current.u_name);
 
//parsing the response
 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
 var responseOBJ = JSON.parse(responseBody);
 gs.info("ResponseBody"+responseBody);
 gs.info("Responseobj"+responseOBJ);
 gs.info("status code"+httpStatus);
 //current.u_name = responseOBJ.name;
 //gs.info(+responseOBJ.result);

Please pour your suggestions into this. Thank you!!



2 ACCEPTED SOLUTIONS

SunilKumar_P
Giga Sage

Hi @Madhan007 , Can you try below?

var responseOBJ = JSON.parse(responseBody);
var name = responseOBJ.result[0].name; // To parse name

 

Regards,

Sunil

View solution in original post

Hi @Madhan007 
You can access the state field in the same way.
Let me give you the structure of JSON in a better way.
It first contain an object then with key result and value to this key is an array with one element which is again an object, so it will always be parsed like obj.result[0].state etc.

{
  "result": [
    {
      "calendar_integration": "1",
      "country": "",
      "user_password": "U3X0xj01Fr",
      "last_login_time": "",
      "source": "",
      "sys_updated_on": "2023-12-11 12:27:28",
      "building": "",
      "web_service_access_only": "true",
      "notification": "2",
      "enable_multifactor_authn": "false",
      "sys_updated_by": "system",
      "sys_created_on": "2023-06-25 12:00:57",
      "sys_domain": {
        "link": "https://dev180310.service-now.com/api/now/table/sys_user_group/global",
        "value": "global"
      },
      "state": "",
      "vip": "false",
      "sys_created_by": "system",
      "zip": "",
      "home_phone": "",
      "time_format": "",
      "last_login": "",
      "default_perspective": "",
      "active": "true",
      "sys_domain_path": "/",
      "cost_center": "",
      "phone": "",
      "name": "Virtual Agent",
      "employee_number": "",
      "password_needs_reset": "false",
      "gender": "",
      "city": "",
      "failed_attempts": "",
      "user_name": "virtual.agent",
      "roles": "",
      "title": "",
      "sys_class_name": "sys_user",
      "sys_id": "4583e347b4672110320f8dc279c804bb",
      "federated_id": "MApWBRP6bOMaUi1JwFN32jJhX72bItv8zP0QwSsOdUQ=",
      "internal_integration_user": "false",
      "ldap_server": "",
      "mobile_phone": "",
      "street": "",
      "company": "",
      "department": "",
      "first_name": "Virtual",
      "email": "",
      "introduction": "",
      "preferred_language": "",
      "manager": "",
      "locked_out": "false",
      "sys_mod_count": "3",
      "last_name": "Agent",
      "photo": "",
      "avatar": "",
      "middle_name": "",
      "sys_tags": "",
      "time_zone": "",
      "schedule": "",
      "date_format": "",
      "location": ""
    }
  ]
}





Thanks and Regards,

Saurabh Gupta

View solution in original post

10 REPLIES 10

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi @Madhan007 

 gs.info("Buddy Responseobj"+JSON.stringify(responseOBJ));  // this is being logged as  [object Object].
//Change as per above 

Thanks and Regards,

Saurabh Gupta

Hi @Madhan007 

With the JSON.parse(obj), it will give you an object.

JSON.stringify() (w3schools.com)

JSON.parse() (w3schools.com)

 


Thanks and Regards,

Saurabh Gupta

Madhan007
Tera Contributor

I Tried this, now instead of [object object], I am getting the same  response body value that I got from API call. I want to parse that result object and access the values inside the object. How can i do that?

 

Hi,
You can get as below

 

 gs.info(responseOBJ.result[0].state)

 




Thanks and Regards,

Saurabh Gupta