REST Data Source

Victor53
Tera Contributor

Hi, I'm using a REST data source to retrieve a JSON payload and feed it through a transform map. I don't have any issues with it other than the fact that the response is of the wrong shape, and I would like to parse it, transform it, update some properties, etc.
I can see that the request action has an option for a preprocessing script, but nothing that would suggest there's a way to parse the response before passing it along to the transform map or whatever other shenaningans SNOW does under the hood. Could you give me some directions, if any?
Thank you.

5 REPLIES 5

Vishal Jaswal
Giga Sage

Hello @Victor53 

 

Your instincts are spot on!

 

The cleanest, most powerful way is to write your own server-side script to consume the REST API, manipulate the response however you want, and then insert/update records into the import table or target table.

 

var r = new sn_ws.RESTMessageV2('Your_Integration_Name', 'get');
var response = r.execute();
var body = response.getBody();

var parsed = JSON.parse(body);

// Modify or reshape payload as needed
var transformed = [];
parsed.data.forEach(function(entry) {
transformed.push({
first_name: entry.firstName,
last_name: entry.lastName,
employee_id: entry.employeeId
});
});

// Now insert into import set table
transformed.forEach(function(item) {
var gr = new GlideRecord('u_my_import_table');
gr.initialize();
gr.setValue('first_name', item.u_first_name);
gr.setValue('last_name', item.u_last_name);
gr.setValue('employee_id', u_employee_id);
gr.insert();
});

 


Hope that helps!

Ankur Bawiskar
Tera Patron
Tera Patron

@Victor53 

If you are talking about the below then

did you try to use Parsing script which can be used to manipulate each line?

AnkurBawiskar_0-1747296295747.png

AnkurBawiskar_1-1747296401070.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur, great find. Logging the line displays the WHOLE response payload, in its entirety, which is roughly looking like this: 

{
"name": "some name",
"type": "some type",
"a" : [
{some obj},
{some other obj},
...
],
"b": [...]
...
}

Here's my confusion.
1. Line is throwing me off, because it's not a "line" of something, but rather the entire json string.
2. What should the result of the script execution contain ? An object ? An array of objects ? How should the output look like for it to go through a transform map and end up as a table record ?

@Victor53 

I haven't played around with it, but check this

I believe each line means single json object and when all the lines combine it's array of objects

How does the Custom (Parse by script) work in data sources? 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader