- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2020 09:19 PM
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!!!!!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2020 06:11 PM
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());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2020 10:10 AM
No change.
Log file says "cannot ready property "0" from undefined.
The outbound HTTP log says;
{"error":{"message":"Exception while reading request","detail":"Cannot decode: java.io.StringReader@139f63c"},"status":"failure"}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2020 12:46 PM
Few questions. From what you shared earlier you created the "str" object with this data that included the outermost notation marks;
However, when I look at the Response Body in my Outbound HTTP Log that isn't there;
So, if I'm not mistaken when my "var response = request.execute();" completes then I'm left with this;
var response = {"import_set":"ISET4030016","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":"3cc1848a07e2761445600e9137a4b0f8","test_display_value":"TEST0235522"}]}
Is that correct? If so I've already taken these steps to ADD the ' marks and it works in the Scripts-Background but still not working in the System Logs.
Before Stringify;
After Stringify;
When I try to stringify the results for my Business Rule though it still isn't working;
org.mozilla.javascript.EcmaError: Cannot read property "0" from undefined
Caused by error in sys_script.4203538d1b7e8c10da7b10e4bd4bcb74.script at line 32
29: var modifiedResponse = "'" + JSON.stringify(response) + "'";
30: var parser = new JSONParser();
31: var parsedResponse = parser.parse(modifiedResponse);
==> 32: gs.log(parsedResponse.result[0].test_sys_id);
33: gs.log(parsedResponse.result[0].test_display_value);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2020 05:48 PM
A few more hours of trying and I've yet to find any combination that will yield the test_display_value and test_sys_id from the following. I've got no issues when going against my responses that I'm using to extract the info on my Incident table, so I'm at a total loss.
{"import_set":"ISET4030016","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":"43be4c1848a07e276ea021136144b05c","test_display_value":"TEST0235533"}]}
Current code.
var response = request.execute();
gs.log(response.getBody()); //confirms in the system log I'm getting a good response
var responseBody = response.getBody();
var parser = new JSONParser();
var parameterArr = parser.parse(responseBody);
gs.log(parameterArr.result[0].sys_id);
Anyone got anything?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2020 06:11 PM
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());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2020 06:24 PM
Ok, that's good to know. I'll have to check in on that more later.
I've actual migrated the whole sha'bang on my side. I'm working from the global space now. I've just created a module inside the request application so I'm just building and sending RITM now. So I don't believe scope should play any part any longer if that is what was messing it up to begin with.
I made the changes and this is what I've got;
Code;
var response = request.execute();
var parser = new global.JSON().decode(response.getBody());
gs.log(parser);
gs.log(parser.result);
gs.log(parser.result.test_display_value);
Consistently I'm just pulling back nested objects that I can't seem to dig through.