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

I did some experimentation and looks like you can parse multiple objects if you combine them in to one array.

 

I used following input parameters in Rest API explorer.

 

find_real_file.png

 

Then, In the scripted rest API I used following code to parse it.

 

var obj = request.body.data;
		gs.log('obj '+ JSON.stringify(obj));
		
		return{
			"short_description": obj[0].short_description,
			"description": obj[0].description,
			"short_description1": obj[1].short_description1,
			"description1": obj[1].description1
		};

 

And I received following response back to my web service call in Rest API explorer.

 

find_real_file.png

 

Let me know if you have any more questions? If this answers you question, please mark the response as correct.

 

Thanks,

Akshay

If my JSON object is as below:

{
"Supplier" :[
{ guid: 1111,
supp_name: 'ABC',
supp_addr: '35'
},
              { guid: 2222,
supp_name: 'PQR',
supp_addr: '45'
}
],
"Contacts" :[
{ "firstName": "Sally",
"LastName": "Kumar",
"guid": 1111 },
             { "firstName": "Priya",
"LastName": "Gupta",
"guid": 1111 }
              ]
}

I want to store Supplier details in Supplier table and Contacts details in Contact table which is child table of Supplier table.

How to achieve it?

Regards,
Anubhav

Hi,

 

I am getting one JSON object as "BIRTH_DATE": "19840103".

However when I am parsing and mapping it in user table it is loading the data exactly like what I mentioned above. I want it in yyyy-mm-dd format. Can you please help me on how to do this?