ServiceNow Parse JSON array
Example JSON response:
"task_instances": [
{
"dag_id": "create-federated-saml-app-poc-v1",
"dag_run_id": "manual__2024-11-06T10:33:28.822918+00:01",
"duration": 25.854218,
"end_date": "2024-11-06T10:34:00.243529+00:01",
}
{
"dag_id": "create-federated-saml-app-poc-v2",
"dag_run_id": "manual__2024-11-06T10:33:28.822918+00:02",
"duration": 25.854217,
"end_date": "2024-11-06T10:34:00.243529+00:02",
}
{
"dag_id": "create-federated-saml-app-poc-v3",
"dag_run_id": "manual__2024-11-06T10:33:28.822918+00:03",
"duration": 25.854216,
"end_date": "2024-11-06T10:34:00.243529+00:03",
}
],
"total_entries": 10
}
Code:
function retrieveDetailedErrorLogs(grIntegrationAlert) {
//--parameter: the u_integration_alerts entry where the error status was reported
var dag_run_id = grIntegrationAlert.u_transaction_id;
var DAG_ID = 'create-federated-saml-app-poc-v1';
var sReturn = '';
try {
var r = new sn_ws.RESTMessageV2('Glidepath', 'GlidePathTaskInstances');
r.setStringParameterNoEscape('dag_run_id', dag_run_id);
r.setStringParameterNoEscape('dag_to_run', DAG_ID);
//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();
sReturn = httpStatus;
gs.print('httpStatus=' + httpStatus);
if (httpStatus == 200) {
var ojson = JSON.parse(responseBody);
var taskInstances = ojson.task_instances; //--looking for 'success'
gs.print('len=' + taskInstances.length);
for (var i = 0; i < taskInstances.length; i++) {
//var dag = jsonData.taskInstances[i];
//gs.print(dag.dag_id);
gs.print(taskInstances[i].dag_id)
}
}
} catch (ex) {
var message = ex.message;
//gs.logError(this.log_prefix + 'retrieveDetailedErrorLogs:ERROR::' + message + ', ' + grIntegrationAlert.u_servicenow_id.number);
}
return sReturn;
}
Comments
Post a Comment