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

I am getting as undefined with gs.info(responseOBJ.result.state) 

Hi @Madhan007 
My bad.
Please try below

 

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

 


Thanks and Regards,

Saurabh Gupta

Its working,but how can i access state field and also I dont understand why the response was in the format of array inside an object and all which is difficult to understand and access. Previously . I used to get just a json object as a response , then I will parse it and access it easily.

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

Hi @Madhan007 



Thanks and Regards,

Saurabh Gupta