How to extract Values/Data from JSON Payload?

Ankita9
Tera Contributor

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

9 REPLIES 9

Adrian Ubeda
Mega Sage
Mega Sage

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);

 

 

If it was helpful, please give positive feedback! ✔
☆ Community Rising Star 22, 23 & 24 ☆

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?

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');
If it was helpful, please give positive feedback! ✔
☆ Community Rising Star 22, 23 & 24 ☆

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.

 

 

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

 

If it was helpful, please give positive feedback! ✔
☆ Community Rising Star 22, 23 & 24 ☆