REST Response Parsing

Mike C2
Kilo Contributor

Hello all!

 

I'm having some issue with parsing a JSON result from a POST call. I've perform the same test in POSTMAN with the same userr profile and I receive the following;

 
{
    "import_set": "ISET1600403",
    "staging_table": "u_test_request",
    "result": [
        {
            "transform_map": "Test Transform",
            "table": "sc_req_item",
            "status": "error",
            "error_message": "Target record not found",
            "status_message": "Order Placed;Row transform ignored by onBefore script",
            "test_sys_id": "6c6e2c55137688588013",
            "test_display_value": "TEST0235449"
        }
    ]
}​

 

The particular chunk of my Business Rule is below;

 

var response = request.execute();

var parser = new JSONParser();

var parsedResponse = parser.parse(response);

gs.print(parsedResponse.import_set + "is the import set");

gs.print(parsedResponse.result + "is the .result");

gs.print(parsedResponse.result[0].test_display_value + "is the TEST DISPLAY VALUE");

gs.print(parsedResponse.result[0].test_sys_id + "is the TEST SYS ID");

 

However I only get back the following in the system log;

"org.mozilla.javascript.EcmaError: Cannot read property "0" from undefined"

 

I've also tried this for my Business Rule and received same error;

 

var parser = new JSONParser();
var responseBody = response.getBody();
var parameterArr = parser.parse(responseBody);
var responseSYSID = parameterArr.result[0].test_sys_id;
gs.print(responseSYSID);

 

I've used a nearly identical chunk of code in order to parse and retrieve the SYS ID and other data from the response when doing all of this to an incident table. Given the JSON in the response is identical I'm not sure why it's not working.

 

Thanks in advance!!!!!

1 ACCEPTED SOLUTION

Tony Chatfield1
Kilo Patron

Hi, I think that JSONParser() is legacy and so not available in a scoped app,
which will be why it works in a background script.

Maybe try something like

https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=JSONScopedAPI

var response = request.execute();
var parser = new global.JSON().decode(response.getBody());

 

View solution in original post

12 REPLIES 12

//Try

gs.log(parser.result[0]);
gs.log(parser.result[0].test_display_value);

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Mike C2
Kilo Contributor

@vkachineni 

@Tony Chatfield 

 

You guys are awesome! I've tried a few dozen variations today. So much so that I've gotten to that dazed and confused state! The following did it though;

 

var response = request.execute();
var parser = new global.JSON().decode(response.getBody());
gs.log(parser.result[0].test_display_value);
gs.log(parser.result[0].test_sys_id);

 

I'll definitely read up on the legacy stuff here, but if you guys have any further information on why it HAD to be this way I'd love to hear it. I find it so odd that I was able to do the parsing a different way on the INCIDENT side.

Hi Mike,

The script I shared was for running in background scripts;

yes there is scope issue for the JSONParser

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