Json code into ServiceNow Table

Royal1
Tera Contributor

i have get the Below data from REST API from another third-party tool, the response like below. 

 

Json format:

 

"totalCount" : 1122,
"results" : [ {
"id" : "198232323",
"id2" : "11122-2-32312312",
"general" : {
"name" : "Apple’s Macoo",
"lastIpAddress" : "1121.2223.222.111",
"lastReportedIp" : "1212.2121.121.212",
"jamfBinaryVersion" : "112110.12121.0-1212121",
"platform" : "Mac",
"barcode1" : null,
"barcode2" : null,
"assetTag" : null,
"remoteManagement" : {
"managed" : true,
"managementUsername" : "adddadasdwd"
},
"supervised" : true,
"mdmCapable" : {
"capable" : true,
"capableUsers" : [ ]
},

 

I want to update these data into a custom table and all the attributes are there in costum tables.

 

Please suggest how can we update these data in the table.

 

 

i tried the below option but it didn't work 

 

var obj = JSON.parse(responseBody);

var chuck = new GlideRecord('table');

chuck.order = obj.id;
chuck.number = obj.name;
chuck.c_id = obj.lastIpAddress;

chuck.insert();
chuck.description = obj.lastReportedIp;

3 REPLIES 3

Devender Kumar
Tera Guru
Tera Guru

Hi @Royal1 ,

Please try the script like below.

var responseObj = JSON.parse(responseBody);

var name = responseObj.name;
var email = responseObj.email;
var age = responseObj.age;

var gr = new GlideRecord('your table');
gr.initialize();

gr.name = name;
gr.email = email;
gr.age = age;

gr.insert();

refer the below link.

https://www.servicenow.com/community/secops-vr-forum/how-to-create-record-in-table-from-json-respons... 

 

If my answer resolves your issue then please mark it as correct and helpful.

Regards,

Devender

no luck , unable to get the data

Community Alums
Not applicable

Hello @Royal1,

 

Please try this :

 var r = new sn_ws.RESTMessageV2('rest_message''function_name');
          var response = r.execute();
          var responseBody = response.getBody();
          var httpStatus = response.getStatusCode();
          if (httpStatus == 200) {
              var uArray = JSON.parse(responseBody);
              for (var i = 0; i < uArray.results.length; i++) {
                  var gr = new GlideRecord("table_name");
                  gr.intialize();
                  gr.order = uArray.results[i].id;
                  gr.number = uArray.results[i].name;
                  gr.c_id = uArray.results[i].lastIpAddress;
                  gr.description = uArray.results[i].lastReportedIp;
                  gr.insert();
 
@Royal1 , Please mark my answer as "Accept as Solution" and "Helpfuls." If it works for you.
 
Thank you!