The CreatorCon Call for Content is officially open! Get started here.

how to extract data from response body.

niveditakumari
Mega Sage

Hello,

How to extract data from response body and set that value in table.

ex - I am calling one API and with that I am getting some records and that are stored in response body. Now I want to extract all the values from response body and set that value in one table.

I am trying to achieve this by schedule job.

Can anyone please help me that how I can extract that values from response body and set that value in our tables.

 

Regards,

Nivedita 

1 ACCEPTED SOLUTION

Hi Nivedita,

the script shared earlier should work; ensure you give correct field names for the 6 fields

try {
var r = new sn_ws.RESTMessageV2('Get Data', 'Default GET');

//override authentication profile
authentication_type ='basic';
r.setAuthenticationProfile(authentication_type, 'efea940d1b408010acf5fc88cc4bcbf7');

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

gs.print(responseBody);
gs.log("response body is" + responseBody);
var obj = JSON.parse(responseBody);

for(var i = 0; i<obj.plannedEvents.length; i++) {

var gr = new GlideRecord('u_outage');
gr.initialize();
gr.u_rollbackplan = obj.plannedEvents[i].rollbackPlan;
gr.u_plannedeventstartdate = obj.plannedEvents[i].plannedEventStartDate;
gr.u_closurecode = obj.plannedEvents[i].closureCode;
gr.u_description = obj.plannedEvents[i].description;
gr.u_plannedeventenddate = obj.plannedEvents[i].plannedEventEndDate;
gr.u_ticketid = obj.plannedEvents[i].ticketId;
gr.u_correlationid = obj.plannedEvents[i].correlationId;
gr.u_impact = obj.plannedEvents[i].impact;
gr.u_executionowner = obj.plannedEvents[i].executionOwner;
gr.u_maintenancetype = obj.plannedEvents[i].maintenanceType;
gr.u_sitelocation= obj.plannedEvents[i].maintenanceType;
gr.u_expecteddowntime= obj.plannedEvents[i].expectedDowntime;
gr.u_activitystatus= obj.plannedEvents[i].activityStatus;

var servicesArray = [];

for(var j = 0; j<obj.plannedEvents[i].services.length; j++) {
var gr = new GlideRecord('services_table');
gr.initialize();
gr.u_serviceidentifier= obj.plannedEvents[i].services[j].serviceIdentifier;
gr.u_protectionstatus= obj.plannedEvents[i].services[j].protectionStatus;
gr.u_servicetype= obj.plannedEvents[i].services[j].serviceType;
gr.u_servicealias= obj.plannedEvents[i].services[j].serviceAlias;
gr.u_customerlename= obj.plannedEvents[i].services[j].customerLeName;
gr.u_cuid= obj.plannedEvents[i].services[j].cuid;
var sysId = gr.insert();    
servicesArray.push(sysId.toString());
}

gr.u_services = servicesArray.toString();
gr.insert();

}
gs.log("table data is" + responseBody);
}


catch(ex) {
gs.info('Exception is: ' + ex);
}
Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

55 REPLIES 55

Hi Nivedita,

the issue I see is you are iterating the services array but it is using same glide record object

Regards

Ankur

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

Hello Ankur,

Thank you for your reply.

I didn't get you. Can you please help me with code where I am doing wrong.

 

Regards,

Nivedita

Hello Ankur,

Could you please help me with this.

 

Regards,

Nivedita

Hi Nivedita,

in the example response you have shown it has 4 services json object; are you saying you want 4 records

these 4 records will have same value for these 13 fields i.e. closureCode,rollbackPlan,plannedEventStartDate,siteLocation,ticketId,description,maintenanceType,impact,correlationId,executionOwner,plannedEventEndDate,expectedDowntime,activityStatus

and different values for these 6 fields i.e. serviceIdentifier,protectionStatus,serviceType,serviceAlias,customerLeName,cuid

If yes the modify code as below and test once

I have put the GlideRecord initialize() inside the services array

try { 
var r = new sn_ws.RESTMessageV2('Get Data', 'Default GET');

//override authentication profile 
authentication_type ='basic';
r.setAuthenticationProfile(authentication_type, 'efea940d1b408010acf5fc88cc4bcbf7');

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

gs.print(responseBody);
gs.log("response body is" + responseBody);
var obj = JSON.parse(responseBody);

for(var i = 0; i<obj.plannedEvents.length; i++) {

for(var j = 0; j<obj.plannedEvents[i].services.length; j++) {

var gr = new GlideRecord('u_outage');
gr.initialize();

// these 13 fields will have same values for all 4 records
gr.u_rollbackplan = obj.plannedEvents[i].rollbackPlan;
gr.u_plannedeventstartdate = obj.plannedEvents[i].plannedEventStartDate;
gr.u_closurecode = obj.plannedEvents[i].closureCode;
gr.u_description = obj.plannedEvents[i].description;
gr.u_plannedeventenddate = obj.plannedEvents[i].plannedEventEndDate;
gr.u_ticketid = obj.plannedEvents[i].ticketId;
gr.u_correlationid = obj.plannedEvents[i].correlationId;
gr.u_impact = obj.plannedEvents[i].impact;
gr.u_executionowner = obj.plannedEvents[i].executionOwner;
gr.u_maintenancetype = obj.plannedEvents[i].maintenanceType;
gr.u_sitelocation= obj.plannedEvents[i].maintenanceType;
gr.u_expecteddowntime= obj.plannedEvents[i].expectedDowntime;
gr.u_activitystatus= obj.plannedEvents[i].activityStatus;

// these 6 fields will have different values for 4 records
gr.u_serviceidentifier= obj.plannedEvents[i].services[j].serviceIdentifier;
gr.u_protectionstatus= obj.plannedEvents[i].services[j].protectionStatus;
gr.u_servicetype= obj.plannedEvents[i].services[j].serviceType;
gr.u_servicealias= obj.plannedEvents[i].services[j].serviceAlias;
gr.u_customerlename= obj.plannedEvents[i].services[j].customerLeName;
gr.u_cuid= obj.plannedEvents[i].services[j].cuid;
gr.insert();

}
gs.log("table data is" + responseBody);
}


catch(ex) {
var message = ex.message;

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hello Ankur,

I am saying that when i run background script code it should create only one row in table like the way it is having in JSON string.

ex - In table only one row should be created and which should have one value for 

closureCode,rollbackPlan,plannedEventStartDate,siteLocation,ticketId,description,maintenanceType,impact,correlationId,executionOwner,plannedEventEndDate,expectedDowntime,activityStatus

and four different values for services because in JSON string services is nested and it contains four different values for 

serviceIdentifier,protectionStatus,serviceType,serviceAlias,customerLeName,cuid

I am using below code, with that values are setting into table but for service it is setting the same value for each services.

 

try {
var r = new sn_ws.RESTMessageV2('Get Data', 'Default GET');

//override authentication profile
authentication_type ='basic';
r.setAuthenticationProfile(authentication_type, 'efea940d1b408010acf5fc88cc4bcbf7');

//set a MID server name if one wants to run the message on MID
//r.setMIDServer('MY_MID_SERVER');

//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
//r.setEccParameter('skip_sensor', true);

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

gs.print(responseBody);
gs.log("response body is" + responseBody);
var obj = JSON.parse(responseBody);

for(var i = 0; i<obj.plannedEvents.length; i++) {
var gr = new GlideRecord('u_outage');
gr.initialize();
gr.u_rollbackplan = obj.plannedEvents[i].rollbackPlan;
gr.u_plannedeventstartdate = obj.plannedEvents[i].plannedEventStartDate;
gr.u_closurecode = obj.plannedEvents[i].closureCode;
gr.u_description = obj.plannedEvents[i].description;
gr.u_plannedeventenddate = obj.plannedEvents[i].plannedEventEndDate;
gr.u_ticketid = obj.plannedEvents[i].ticketId;
gr.u_correlationid = obj.plannedEvents[i].correlationId;
gr.u_impact = obj.plannedEvents[i].impact;
gr.u_executionowner = obj.plannedEvents[i].executionOwner;
gr.u_maintenancetype = obj.plannedEvents[i].maintenanceType;
gr.u_sitelocation= obj.plannedEvents[i].maintenanceType;
gr.u_expecteddowntime= obj.plannedEvents[i].expectedDowntime;
gr.u_activitystatus= obj.plannedEvents[i].activityStatus;

 

for(var j = 0; j<obj.plannedEvents[i].services.length; j++) {
gs.print("##Service values###"+obj.plannedEvents[i].services[j].serviceIdentifier);
gs.print("##Service values###"+obj.plannedEvents[i].services[j].protectionStatus);
gs.print("##Service values###"+obj.plannedEvents[i].services[j].serviceType);
gs.print("##Service values###"+obj.plannedEvents[i].services[j].serviceAlias);
gs.print("##Service values###"+obj.plannedEvents[i].services[j].customerLeName);
gs.print("##Service values###"+obj.plannedEvents[i].services[j].cuid);
}
}

//gs.print("##planned Description##"+obj.plannedEvents.length);
//gs.print("##Service###"+obj.plannedEvents[0].services.length);
//gs.print("##Hello##"+obj.message);

//var parser = new global.JSON();
//var parsed = parser.decode(responseBody);
//gs.log("Parsed Data1:"+parsed.result.siteLocation);

}

catch(ex) {
var message = ex.message;

}

I have created four different tabs to set four different values for services but in all services tab i am getting the same value for 

serviceIdentifier,protectionStatus,serviceType,serviceAlias,customerLeName,cuid

It should be different for every services tab.

find_real_file.png

 

Regards,

Nivedita