Parsing JSON From REST API Request

Mike C2
Kilo Contributor

Hello all. Long time lurker, first time poster!

 

To start with, I appreciate everything I've been able to take in over the years. It's only because of this Community that I'm at the point I am now, and whilst that is a "point" of trouble I'm appreciative that I'm at a place of asking for help.

 

Long story short, I'm working to set up a ServiceNow to ServiceNow integration. What I've currently done;

- On target instance, table has been created, extends import set, necessary fields populated, transform map built

- On target instance, REST user has been created, roles provisioned correctly, validated with REST API EXPLORER that user can provision new tickets

- On source instance the Outbound REST message has been created for a POST to the staging table, the "CONTENT" block of the HTTP request is populated with variables

- On source instance, a business rule has been created to run "ON INSERT" to Incident table in source. It replaces the variables of the HTTP request block with the ticket information and sends it, as well as captures the response and logs information from target instance into the ticket on source instance.

 

 

So far EVERYTHING is working except I cannot gather the SYS_ID and NUMBER from the response. Below is the code from my business rule. I'm definitely weak on this subject matter but working to correct that. In checking the syslog I see a few lines of entry that indicate "org.mozilla.javascript.EcmaError: Empty JSON string" so I'm pretty sure my issues reside in parsing the JSON.

 

 

 

 

(function executeRule(current, previous /*null when async*/) {

// Add your code here
var request = new sn_ws.RESTMessageV2("Target ServiceNow Instance", "create_incident");
request.setStringParameterNoEscape("presincnum", current.number);
request.setStringParameterNoEscape("presincsys", current.sys_id);
request.setStringParameterNoEscape("abrincnum", current.u_client_incident_number);
request.setStringParameterNoEscape("shodes", current.short_description);
request.setStringParameterNoEscape("des", current.description);
request.setStringParameterNoEscape("incsta", current.incident_state);
request.setStringParameterNoEscape("imp", current.impact);
request.setStringParameterNoEscape("urg", current.urgency);
request.setStringParameterNoEscape("pri", current.priority);
request.setStringParameterNoEscape("connam", current.caller_id.name);
request.setStringParameterNoEscape("conema", current.caller_id.email);
request.setStringParameterNoEscape("conite", current.cmdb_ci);
request.setStringParameterNoEscape("addcom", current.comments);
request.setStringParameterNoEscape("rescod", current.close_code);
request.setStringParameterNoEscape("resnot", current.close_notes);
request.setStringParameterNoEscape("respar", current.u_sync_ticket);

var response = request.execute();
var requestBody = request.getRequestBody();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

parsedJSON = JSON.parse(responseBody);

var result = parsedJSON["result"];
var targetTicNumber = result[0]["display_value"];
var targetSys_id = result[0]["sys_id"];
current.u_client_sysid = targetSys_id;
gs.log(targetSys_id);

})(current, previous);

 

1 ACCEPTED SOLUTION

The last thing I can think of would be to perhaps execute the process using credentials with admin and see if that makes any difference. If it does, it might be a role or something getting in the way. If it doesn't work with admin, I'd review the logs on your target instance. Maybe an error is occurring that doesn't stop the record creation, but stops the response?

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

View solution in original post

22 REPLIES 22

Michael Jones -
Giga Sage

Have you validated what response you're getting?

I'd probably start by adding a line before your parse statement to see what you are working with. 

gs.info('response is: ' + responseBody);

parsedJSON = JSON.parse(responseBody);

And then work from there....

 

If this was helpful or correct, please be kind and click appropriately!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Mike,

first of all print the json string and then accordingly parse it

var responseBody = response.getBody();

gs.info('Response is: ' + responseBody);

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

ExciteBikeMike
Kilo Explorer

@Ankur Bawiskar 
@michael jones

 

Thanks for the responses on this!

 

When I check the LOG ENTRY after making these modifications I get a message "response is: " and it's totally blank.

 

When I check the HTTP outbound log for the POST there is no data at all under the "RESPONSE" tab.

 

I should be receiving a response by default correct? That was my impression at least.

 

When I perform a "TEST" from the REST API EXPLORER in the TARGET system against the import set table I receive a "RESPONSE BODY" without issue.

Mike C2
Kilo Contributor

@Ankur Bawiskar / Michael Jones

 

Thanks for the replies!

 

I tested that out and I'm just getting "response is: " in the event log. Additionally the HTTP requests logs shows the "response" totally blank.

 

Am I doing something wrong in the SOURCE instance? I was under the impression that a successful API call out would have a return value. I've validated that I'm DEFINITELY getting tickets being created in the TARGET instance.