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

How to extract data from array within Response Body

cstone
Tera Contributor

Hello Everyone - 

I'm relatively new to ServiceNow Development and scripting in general.  Learning as I go so to speak.

We are setting up an integration with a 3rd party application using outbound REST Messages.

In general it is going well, but there is one part giving me the run around and I was hoping someone here could offer some advice.

I am trying to capture a value from the Response Body and feed it into a custom field on our incident table, something I have done before successfully, but this time I am having issues getting to the data

Here is the relevant part of my script in the business Rule:

var response = r.execute();
var json = response.getBody();
var obj = JSON.parse(json);
var targetNumber = obj.items.ticketId; // the line causing my headaches
current.u_external_ticket_number = targetNumber;
current.u_integration_name = "Example";
current.update();

The "Integration Name" field is being set correctly, but I get nothing in the "External Ticket Number" field.

The response body is:

{"items":[{"ticketId":6276389,"inventoryId":11748563,"wtn":"2125551212","errorMessage":"Error Message Would Show Here","errorCode":409}]}

I imagine its the [ ] causing an issue with the parsing.  An array inside an object?  Not something I have had to work through until now.

Could someone point me in the right direction on how to pull "ticketId" out of the "items" object as a string to feed into the "u_external_ticket_number" field?

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi, Below please find updated code.

Note: Untested code.

var response = r.execute();
var obj = response.getBody();

var str = JSON.stringify(obj);

var parser = new JSONParser();

var result = parser.parse(str);

var targetNumber = result.items[0].ticketId; 
current.u_external_ticket_number = targetNumber;
current.u_integration_name = "Example";
current.update();

View solution in original post

5 REPLIES 5

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi, Below please find updated code.

Note: Untested code.

var response = r.execute();
var obj = response.getBody();

var str = JSON.stringify(obj);

var parser = new JSONParser();

var result = parser.parse(str);

var targetNumber = result.items[0].ticketId; 
current.u_external_ticket_number = targetNumber;
current.u_integration_name = "Example";
current.update();

Thank you so much for your assistance @Pradeep Sharma !  

This is working great.  Now that I see it written out I am kicking myself.  Not at all as complicated as I thought it was going to be.

But I guess that is how you learn and improve.

Thanks again to yourself and all the others who replied!  This community is amazing!

Geoff_T
Mega Sage

Hello,

You can access the array within the object like:

// where items is the array and [0] denotes the first index of the array
obj.items[0].ticketId;

You will want to know if this response data structure will always be the same mind you, otherwise you might need a more dynamic scripting solution i.e. if the number of items in the array was not always the same etc

I hope this helps.

 

 

Geoff

ChrisBurks
Giga Sage

The answers above will work fine if you are sure that you will always get only one object in that array.

So be sure to learn about the different types of iterators that can be used with javascript array built-in methods or at least the for loop.

Javascript array methods
Javascript for loop or looping in general

If you learn those you can script to compensate whether one item is in an array or multiple.

Also the method mentioned in the above replies will error if no items came back in the response.