- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 01:44 AM - edited 03-27-2025 01:51 AM
I've been attempting to parse an outbound rest message response in JSON format from a third party alerting tool, however the use of JSON.parse doesn't seem to work, showing values as 'undefined' in the logs.
Here is a snippet of the response which I've amended slightly for security reasons -
27-03-2025 08:37:04 { "data": [ { "id": "2e3a54dd-358e-4290-92ef-08dc666ad290", "ref": "WXXZ920", "name": "AE_[DNT_OT] Forescout - Anomalous Network Communication", "siemCreatedDate": "2025-03-26T13:14:36.307Z", "createdDate": "2025-03-26T13:17:35.3553433Z", "priority": "P4", "assignedTo": "Unassigned", "status": "WithCustomer", "respondedDate": "2025-03-26T13:17:59.387Z", "respondByDate": "2025-03-26T16:17:35.3553433Z", "lastModifiedDate": "2025-03-26T13:20:27.6829699Z" }, { "id": "748b7919-a95c-46c4-9830-48dd566ad290", "ref": "FISTBFM", "name": "AE_[DRP_OT] Fout - M in the M", "siemCreatedDate": "2025-03-25T23:15:43.231Z", "createdDate": "2025-03-25T23:18:22.9764057Z", "priority": "P4", "assignedTo": "Unassigned", "status": "WithCustomer", "respondedDate": "2025-03-25T23:18:50.313Z", "respondByDate": "2025-03-26T02:19:22.9764056Z", "lastModifiedDate": "2025-03-25T23:22:00.9125494Z"} ], "total": 121, "currentPage": 0, "pageSize": 2 }
Script I'm using (within a scheduled script execution)
try {
var r = new sn_ws.RESTMessageV2('x_gccw2_cybiquity.Cybiquity', 'Get List');
//override authentication profile
//authentication type ='basic'/ 'oauth2'
//r.setAuthenticationProfile(authentication type, profile name);
//set a MID server name if one wants to run the message on MID
//r.setMIDServer('MY_MID_SERVER');
//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
//r.setEccParameter('skip_sensor', true);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log (responseBody);
var parsedData = JSON.parse(responseBody);
gs.log(parsedData.data);
}
catch(ex) {
var message = ex.message;
}
Any dot walking beyond parsedData.data returns 'undefined' as a value.
I need to be able to parse the data into its individual components and then use this to create incident records but I can't see what I'm missing, so any help would be greatly appreciated!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 02:15 AM
Hello @Dave Bressingto ,
Please replace the two occurrences of "parsedData.result" with "parsedData.data". And there's also a typo there: "prasedData".
for (i = 0; i < parsedData.data.length; i++) {
gs.log("Alert:" + parsedData.data[i].name);
}
Regards,
Robert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 01:55 AM
Hello Dave,
Is the "gs.log (responseBody);" line printing the snippet string that you have put at the top of your post?
If so, can you show the code where you are actually accessing the elements of "parsedData.data"?
Just to make sure, this is an array, so are you looping through those elements? Something like this?
parsedData.data.forEach(element => {
gs.log(element.id);
});
This is working fine on my end:
*** Script: 2e3a54dd-358e-4290-92ef-08dc666ad290
*** Script: 748b7919-a95c-46c4-9830-48dd566ad290
Regards,
Robert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 02:07 AM
Hi Robert, thanks for your reply.
Yes, the gs.log(responseBody) does produce the snippet string I've posted.
Sure, this is what I had written to loop through the data:
try {
var r = new sn_ws.RESTMessageV2('x_gccw2_cybiquity.Cybiquity', 'Get List');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log (responseBody);
var parsedData = JSON.parse(responseBody);
gs.log(parsedData);
for(i=0;i<parsedData.result.length;i++)
{
gs.log("Alert:" + prasedData.result[i].name);
}
}
catch(ex) {
var message = ex.message;
}
I'm expecting it to print the 'name' element from the JSON response, one after the other for the two examples in that response.
Am I missing something obvious?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 02:12 AM
Hello @Dave Bressingto, can you change your for loop as below and then try:
for(i=0;i<parsedData.data.length;i++)
{
gs.log("Alert:" + prasedData.data[i].name);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 02:15 AM
Hello @Dave Bressingto ,
Please replace the two occurrences of "parsedData.result" with "parsedData.data". And there's also a typo there: "prasedData".
for (i = 0; i < parsedData.data.length; i++) {
gs.log("Alert:" + parsedData.data[i].name);
}
Regards,
Robert