We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Unable to parse outbound rest message response

Dave Bressingto
Tera Contributor

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!

 

1 ACCEPTED SOLUTION

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

View solution in original post

7 REPLIES 7

Hi Robert,

That has worked, thank you very much! 

I also have to thank Nishant8 who was also on the same path, but missed the simple typo which was my fault really.

 

This was my first community post, and I have to say, really impressed with the speed at which people respond, so it won't be my last!

 

Thanks again both.

Nishant8
Giga Sage

Hello @Dave Bressingto , Response is an array and you will have to walk thru each object inside. can you please try below:

for (var i=0; i<parsedData.data.length; i++) {
gs.info("Value:" + parsedData.data[i].id);
}

Ankur Bawiskar
Tera Patron

@Dave Bressingto 

try this and identify the issue

Are you sure the time when you tested this code, the Response came as valid JSON

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("Response Body: " + responseBody);

    parsedData = JSON.parse(responseBody);
    gs.log("Parsed Data: " + JSON.stringify(parsedData));

    // Accessing the data array
    var dataArray = parsedData.data;
    gs.log("Data Array: " + JSON.stringify(dataArray));

    // Loop through the data array and log individual components
    for (var i = 0; i < dataArray.length; i++) {
        var item = dataArray[i];
        gs.log("Item " + i + ": " + JSON.stringify(item));
        gs.log("ID: " + item.id);
        gs.log("Name: " + item.name);
        // Add more fields as needed
    }
} catch (ex) {
    var message = ex.message;
    gs.log("Error: " + message);
}

When I used the same JSON and used in background script, it worked for me

try {
    var jsonResponse = '{"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}';

    // Parse the JSON response
    var parsedData = JSON.parse(jsonResponse);

    // Loop through the data array and log individual components
    for (var i = 0; i < parsedData.data.length; i++) {
        var item = parsedData.data[i];
        var assignedTo = item.assignedTo;
        var name = item.name;
        var ref = item.ref;
        var createdDate = item.createdDate;

        gs.log("Assigned To: " + assignedTo);
        gs.log("Name: " + name);
        gs.log("Ref: " + ref);
        gs.log("Created Date: " + createdDate);
    }
} catch (ex) {
    var message = ex.message;
    gs.log("Error: " + message);
}

Output:

AnkurBawiskar_0-1743066992311.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader