REST API: I am getting response data in json when i test the 'get' method, how to add these data to table?

swathigangadhar
Tera Expert

I have a third party endpoint, when i save it am getting only 'Default Get' method(no other methods are generated), when i test, am able to get all user details in 'Response' field (json format) , now i want to add these records to my sys_user table, how to do that?

1 ACCEPTED SOLUTION

Hi Swathi,



you need to parse the response using JSONParser() and iterate over the key value pairs


Can you share the response so that the script can be designed?



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

11 REPLIES 11

Hi Swathi,



First of all you have to remove the slash from the response so that you can parse the JSON



var jsonString = "{\"UserID\":\"10001460\",\"Name\":\"B. Swathi\",\"FIrst Name\":\"B.\",\"Last Name\":\"Swathi\",\"Email\":\"\",\"Company\":\"ABC Corp Limited\",\"Company Code\":\"ABC0002\",\"Building\":\"Nagar\",\"City\":\"Cp Dist\",\"Country\":\"INDIA\",\"Department\":\"Support\",\"Vertival\":\"Admin\",\"Sub Vertical\":\"Admin\",\"Gender\":\"Male\",\"Business Phone\":\"\",\"Location\":\"India\",\"Manager\":\"Ashok\",\"State\":\"Kar\",\"Street\":\"kur\",\"Time Zone\":\"\",\"VIP\":\"\",\"Pincode\":\"500\"}";



jsonString = jsonString.replaceAll("\\","");



var parser = new JSONParser();


var parsedData = parser.parse(jsonString);



var userId = parsedData.UserID;


var Name = parsedData.Name;



gs.print(userId);


gs.print(Name);



this prints the userId and name in background script. execute this in background script



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Swathi,



Once you get all the values such as name, userId, location etc then you can populate it in sys_user table



var gr = new GlideRecord('sys_user');


gr.initialize();


gr.user_name = userId;


gr.location = location


.


.


.


gr.insert();



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

try {


var r = new sn_ws.RESTMessageV2('Test', 'Default GET');


var response = r.execute();


var responseBody = response.getBody();


var httpStatus = response.getStatusCode();



  var parser = new global.JSON();


  var parsed = parser.decode(responseBody);  


var gr = new GlideRecord('sys_user');  


gr.initialize();  


gr.name = parsed.Name;  


gr.u_user_id = parsed.UserID;  


gr.insert();  


}


catch(ex) {


var message = ex.getMessage();


}


===============================================



Hi Ankur,



am using above script in my scheduled jobs, but record is getting created in sys_user table, but name and user_id fields are not populating, every thing is blank....


What am doing wrong?




-------


Am getting the responsons properly....


This the response when i test get message:


{\"UserID\":\"10001111\",\"Name\":\"Swathi\"}


Hi Swathi,



Did you print the jsonString?


Try following line and print the values



var parser = new JSON().decode(responseBody);


gs.print(parser.UserID);


gs.print(parser.Name);



First try to print the values and then insert into user table



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur, am getting 100+ records, just i copied one record.. How to add all other record?