Skip to main content

ServiceNow Parse JSON Array

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

Popular posts from this blog

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){...

Code a pause/wait - gs.sleep or gs.wait alternative, pause script for specified seconds (timer)

Code a pause/wait - gs.sleep / gs.wait alternative, pause script for specified seconds (timer)  e.g. 10 seconds: do_sleep ( 10000 ); function do_sleep ( milliseconds ) { var start = new Date (). getTime (); for ( var i = 0 ; i < 1e7 ; i ++) { if (( new Date (). getTime () - start ) > milliseconds ){ gs . print ( 'waking up!' ); break ; } } }