How to retrieve JSON objects using REST API web services and push date to Snow tables.

Anubhav Ritoli3
Giga Contributor

I have to create Web service in ServiceNow. 

Client will using this web service and call method with JSON object parameter.

I want to parse this JSON object in servicenow and push the record details in Servicenow Staging tables.

Please advice or share any relavant details or link for understanding.

Regards,

Anubhav

1 ACCEPTED SOLUTION

Akshay14
Tera Guru

Hi Anubhav,

I worked on something like this And posting the same below. 

Create a Scripted Rest API for POST request and paste following code in to the script.

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
	
	// implement resource here
	
	try{
		
		// get hold of request JSON object
		var obj = request.body.data;
		var record = new GlideRecord('u_service_catalog_staging');
		record.initialize();
		record.u_short_description = obj.short_description;
		record.u_description = obj.description;
		record.u_due_date = obj.dueDate;
		record.u_requested_for = obj.requested_for;
		record.u_opened_by = obj.opened_by;
		record.insert();
		
		var gr = new GlideRecord('u_service_catalog_staging');
		if(gr.get(record.sys_id)) {
			return {
				"Response":"Request created Successfully."
			};
		}
		else{
			return{
				"Response":"Request not created"
			};
		}
	}
	
	catch(e){
		gs.log('Something wronng with request paramaters' + e);
	}
	
})(request, response);

Once done. click on the Explore REST API related links to open it up. You can test the functionality from here. Refer below screen shot for the same. 

Let me know if you have questions.

Note - You need to change the staging table name in script to create record.

 

Thanks,

Akshay

View solution in original post

7 REPLIES 7

Saikiran Gudur1
Mega Guru

Hi

This is an example. You need to parse the JSON response.

Parsing makes the responsejson string to an object. Now you can access the object after parsing to retrieve the exact data.


       var r = new sn_ws.RESTMessageV2('REST_name_xxxx', 'get'); // in my case, we are using Get method

       var response = r.execute();

       var responseBody = response.getBody(); //fetches JSON response

       var response= JSON.parse(responseBody);


       for(i=0;i<response.length;i++)

         {

                             var gr = new GlideRecord('u_labname');

                           gr.initialize();

                         gr.u_fieldname = response[i].SiteName;

                           ....................

                         ...................

                       gr.insert();

         }

 

Akshay14
Tera Guru

Hi Anubhav,

I worked on something like this And posting the same below. 

Create a Scripted Rest API for POST request and paste following code in to the script.

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
	
	// implement resource here
	
	try{
		
		// get hold of request JSON object
		var obj = request.body.data;
		var record = new GlideRecord('u_service_catalog_staging');
		record.initialize();
		record.u_short_description = obj.short_description;
		record.u_description = obj.description;
		record.u_due_date = obj.dueDate;
		record.u_requested_for = obj.requested_for;
		record.u_opened_by = obj.opened_by;
		record.insert();
		
		var gr = new GlideRecord('u_service_catalog_staging');
		if(gr.get(record.sys_id)) {
			return {
				"Response":"Request created Successfully."
			};
		}
		else{
			return{
				"Response":"Request not created"
			};
		}
	}
	
	catch(e){
		gs.log('Something wronng with request paramaters' + e);
	}
	
})(request, response);

Once done. click on the Explore REST API related links to open it up. You can test the functionality from here. Refer below screen shot for the same. 

Let me know if you have questions.

Note - You need to change the staging table name in script to create record.

 

Thanks,

Akshay

Hi Akshay,

 

If more then 1 object say 4 objects are passed as parameters, then how to accept it and parse it to PUSH.?

Hello Anubhav,

 

Can you post a sample object structure here so that I can try parsing it?

 

Thanks,

Akshay