Parse JSON in inbound REST

lakshmin
Giga Guru

HI community

We are integrating our Purchase order system to the 3rd party vendor . We need to be able to track the status of our orders . The vendor wants us to publish an API which can take their JSON input and update the status and other relevant tracking information in SNow .

I was thinking to develop a Import set API and ask them to POST the status and tracking information , but looking at the JSON response im thinking  i need a Scripted web service 

There could be multiple Line items per order . So in the JSON response i have to parse to the individual Line items array and get the relevant status and tracking  and update it.

Can you please guide me about the approach and if have to use the Scripted web service point me how to parse individual POL sections in Json response

 

Thanks

Lakshmi 

 

1 REPLY 1

barcar
Tera Contributor

I had a similar requirement to ingegrate with an Oracle service. I created a very simple Scripted Rest API (https://docs.servicenow.com/bundle/newyork-application-development/page/integrate/custom-web-services/concept/c_CustomWebServices.html).

It has an ACL so that only they can authenticate and get access to it. It accepts application/json or XML content.

I created a resource to process POST requests and it contains code to extract the JSON from the request:

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var requestBody = request.body;
    var requestData = requestBody.data;
    var requestString = JSON.stringify(requestData);​
    gs.info("oracle_mc API request {0}", requestString);

You can then expand the script in the resource to iterate through the request with something like

for ( var i = 0; i < requestData.order.line.length; i++) {

   var orderLine = requestData.order.line[i];
   gs.info("Processing order line {0}", orderLine);
   // do something with you order line

}

Of course you need to define what your JSON payload needs to look like and you need to code to make sure that what you have received is compliant with that.

The online documentation linked aboce is a pretty good starting point.

Hope that helps.