Issue with JSON.parse() in a scoped application

Brendan Hallida
Kilo Guru

Hi all,

I am creating a mail script that will add all of the impacted services and affected CI's to a list.  it contains 2 glide queries to the following tables:

  • Affected CIs: task_ci
  • Impacted Services: task_cmdb_ci_service 

I then populate 2 arrays and then parse them into template.prints for formatting.  The Affected CIs parsing is working fine, however, I get the following error regarding the Impacted Services parse.

Evaluator: org.mozilla.javascript.EcmaError: Unexpected token: J

Please note - this mail script is in the Major Incident application scope.

Here is the mailscript:

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    var affectedCIArray = [];
    var impactedArray = [];

    // Glide into the affected cis relationship table
    var affected = new GlideRecord('task_ci');
    affected.addQuery('task', current.incident_alert.source_incident.sys_id); // Need to work out how to get the incident sys_id dynamically
    affected.query();

    // Push affected cis to array
    while (affected.next()) {
        affectedCIArray.push(affected.ci_item.getDisplayValue());
    }

    // Glide into the impacted services relationship table
    var impacted = new GlideRecord('task_cmdb_ci_service');
    impacted.addQuery('task', current.incident_alert.source_incident.sys_id); // Need to work out how to get the incident sys_id dynamically
    impacted.query();

    // push impacted services to array
    while (impacted.next()) {
        impactedArray.push(impacted.cmdb_ci_service.getDisplayValue());
    }

    // logging
    gs.info('VF | affectedCIArray: ' + affectedCIArray);
    gs.info('VF | impactedArray: ' + impactedArray);

    template.print("<p>Affected CIs:</p>");

    for (var i = 0; i < affectedCIArray.length; i++) {
        // Uplift the use of the JSON parser to accommodate scoped applications
        var parser = new global.JSON();
        var ciParsed = JSON.parse(affectedCIArray[i]);
        gs.info('VF | ciParsed: ' + ciParsed);

        if (ciParsed != "") {
            template.print('<div style="line-height: 17pt;">');
            template.print(ciParsed);
            template.print('</div>');
        }
    }

    template.print("<br/>");

    template.print("<p>Impacted Services:</p>");

    // this for loop is not working
    for (var j = 0; j < impactedArray.length; j++) {
        gs.info('VF | impactedArray[j]: ' + impactedArray[j]);
       
        var servicesParsed = JSON.parse(impactedArray[j]);
        gs.info('VF | servicesParsed: ' + servicesParsed);
        if (servicesParsed != "") {
            template.print('<div style="line-height: 17pt;">');
            template.print(servicesParsed);
            template.print('</div>');
        }
    }
})(current, template, email, email_action, event);

It is odd, as with the logging, the line gs.info('VF | impactedArray[j]: ' + impactedArray[j]); is logging correctly, however, the error suggests that it is empty?

find_real_file.png

I have tested this in a background script by itself as well... I get the Same error.  below is the background script:

    var impactedArray = [];
    var impacted = new GlideRecord('task_cmdb_ci_service');
    impacted.addQuery('task', '1d8ea6b41b54b090166542ebdc4bcb87'); // Incident sysID.
    impacted.query();

    // push impacted services to array
    while (impacted.next()) {
        impactedArray.push(impacted.cmdb_ci_service.name);
    }

    // logging
    gs.info('impactedArray: ' + impactedArray);

       // this for loop is not working
    for (var j = 0; j < impactedArray.length; j++) {
        gs.info('impactedArray[j]: ' + impactedArray[j]);
        // Uplift the use of the JSON parser to accommodate scoped applications
        var servicesParser = new global.JSON();
       var servicesParsed = JSON.parse(impactedArray[j]);
        //gs.print('servicesParsed: ' + servicesParsed);
        //if (servicesParsed != "") {
            //gs.print('servicesParsed is working');
        //}
    }

I am stumped here.  I'm sure there is something minor I am missing, but for the life of me, I cannot find it haha.

1 ACCEPTED SOLUTION

From the logs it looks like you are trying to parse an array of strings. If that's the case then there is no need to parse it. Just loop through the array and output the string. Like so: var servicesParsed = impactedArray[j];

View solution in original post

5 REPLIES 5

Glad that I could help. The reason you were receiving that error for the secord parse but not the first was due to the data it was attempting to actually parse. From your logs the first array contained: 1112992,1136184. Both of those numbers are valid JSON formats so running JSON.parse() against either would return the value. But the second array contained the following strings: Jira (PAAS), SAP HR. Neither of those are valid JSON formats hence why you received the error. The first value it attempted to parse was "Jira (PAAS)". Since that string begins with the letter "J" you received the error: 'Unexpected token: J'.

 

Cheers,

David