How to extract Values/Data from JSON Payload?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2022 06:36 AM - edited 11-03-2022 06:44 AM
Hi All,
How can we get the data/values passed in a JSON payload into an OnAfter transform Script?
For eg : Payload
{"u_class":"MS SQL DataBase","u_operation":"Update","u_ci_identifier":"CI000082","u_first_level_support":"ABA-SUPPORT","u_support_hours":"Server"}
I want to get these 2 values("ABA-SUPPORT" and "Server") from the payload and store it in an array in OnAfter Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2022 06:47 AM
Hi Ankita,
You can declare that body into a variable and then access all the values using the keys, I've done this using BG script so in a TMap should work as well:
var data = {
"u_class":"MS SQL DataBase",
"u_operation":"Update",
"u_ci_identifier":"CI000082",
"u_first_level_support":"ABA-SUPPORT",
"u_support_hours":"Server"
};
var arr = [];
arr.push(data.u_first_level_support);
arr.push(data.u_support_hours);
☆ Community Rising Star 22, 23 & 24 ☆
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2022 07:00 AM
Hi Adrian,
Thanks for your response.
But the JSON Payload will be sent via Postman/3rd party tool, Whenever the payload is sent, "var data" should auto populate with the current payload passed, How can we achieve that?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2022 07:14 AM
I'm not sure if I'm understanding the scenario. Are you receiving that JSON or do you want to send it once the TMap onAfter is completed?
If you want to send requestBody into HTTP endPoint, here's a code as a example:
var requestBody = {
"variable1": "value1",
"variable2": value2
};
var r = new sn_ws.RESTMessageV2('API Name', 'Api Method');
r.setRequestBody(JSON.stringify(requestBody));
r.setRequestHeader('Accept', 'application/json');
r.setRequestHeader('Content-Type', 'application/json');
☆ Community Rising Star 22, 23 & 24 ☆
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2022 07:32 AM
Hi Adrian,
I am the Receiver, I will be receiving the JSON Payload and I want to store it somewhere(may be in the Staging table) and access it later on so that I compare the values passed in the payload.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2022 07:54 AM
Thanks Ankita,
I'll store that data in the staging table and then compare it with the data. This documentation maybe is helpful: https://developer.servicenow.com/dev.do#!/reference/api/rome/rest/c_ImportSetAPI
Then, when the post it's done you can access into data using the result array that it's generated
☆ Community Rising Star 22, 23 & 24 ☆