Skip to main content

ServiceNow: retrieve a JSON Array element by its KEY value

 in this example, we wish to retrieve the blue highlighted key value from the JSON array tagsOfAffectedEntities:

{
  "ProblemDetailsJSON" : {
    "id" : "xxxx",
    "startTime" : 1652690280000,
    "endTime" : 1652690280000,
    "displayName" : "yyyy",
    "impactLevel" : "INFRASTRUCTURE",
    "status" : "CLOSED",
    "severityLevel" : "RESOURCE_CONTENTION",
    "commentCount" : 3,
    "tagsOfAffectedEntities" : [ {
      "context" : "CONTEXTLESS",
      "key" : "Application"
    }, {
      "context" : "CONTEXTLESS",
      "key" : "SZCOFF",
      "value" : "SZCOFF12345"
    }, {
      "context" : "CONTEXTLESS",
      "key" : "K8s container xxx",
      "value" : "xxxx"
    }, {
      "context" : "CONTEXTLESS",
      "key" : "[Kubernetes]namespace",
      "value" : "xxx"
    }, {
      "context" : "CONTEXTLESS",
      "key" : "Environment",
      "value" : "xxx"
    }, {
      "context" : "CONTEXTLESS",
      "key" : "NS",
      "value" : "xxx"
    }, {
      "context" : "CONTEXTLESS",
      "key" : "Tier"
    }, {
      "context" : "CONTEXTLESS",
      "key" : "Department",
      "value" : "xxx"
    } ],
  "ProblemID" : "xxx",
  "ProblemImpact" : "xxx",
  "ProblemSeverity" : "xxx",
  "ProblemTitle" : "xxx",
  "ProblemURL" : "https://xxxx.live.dynatrace.com/#problems/problemdetails;pid=-xxx",
  "State" : "RESOLVED",
  "Tags" : "xxx",
  "correlation_id" : "xxx",
  "name" : "xxx"
}

 

use this script:

var sPayload=JSON.parse (< above_payload>); //(assume it's stored in a ServiceNow table field such as em_alert.additional_info) 

example:

var grAlert=new GlideRecord('em_alert');
if (grAlert.get('number', 'Alert0017xxx')){

 
var sPayload=JSON.parse (grAlert.additional_info);

   var svc=_getSvcoffNumber(sPayload);

gs.print(svc);


function _getSvcoffNumber(additionalInfo) {
        
       
        var sReturn = '';
        try {
            var tagsOfAffectedEntities = additionalInfo.ProblemDetailsJSON.tagsOfAffectedEntities;
            sReturn=_extractTags(tagsOfAffectedEntities, "SZCOFF");
             
        } catch (ex) {
            
        }
        return sReturn;
    }

function  extractTags (pTagObject, pTag) {
        
        var vTag;
        try {
            for (i = 0; i < pTagObject.length; i++) {
                if (pTagObject[i]['key'] == pTag) {
                    vTag = pTagObject[i]['value'].toString();
                }
            }
        } catch (ex) {
           
        }
        return vTag;
    }

 ---OUTPUT:

*** Script: SZCOFF12345

 (ServiceNow )

Comments

Popular posts from this blog

URL link in addInfoMessage

var ga=new GlideAjax('gld_HR_ajax'); ga.addParam('sysparm_name', 'checkEmployeeNumber_hrProfile'); ga.addParam('sysparm_hrprofilenumber', g_form.getValue('number')); ga.addParam('sysparm_employeenumber', newValue); ga.getXMLAnswer(function(answer) { if (answer!='undefined' && answer!=''){ var navURL="<a style='text-decoration:underline;color:blue' href=hr_profile.do?sysparm_query=number=" + answer + ">" + answer + "</a><img width='3' src='images/s.gif'/>"; var sMsg='The employee number entered already exists on another HR Profile ' + navURL; //alert(sMsg); g_form.showErrorBox('employee_number', 'error - please check'); g_form.addInfoMessage(sMsg); } });

GlideRecord setValue

setValue(String name, Object value) Sets the specified field to the specified value. Normally a script would do a direct assignment, for example,  gr.category = value . However, if in a script the element name is a variable, then  gr.setValue(elementName, value)  can be used. When setting a value, ensure the data type of the field matches the data type of the value you enter. This method cannot be used on journal fields. If the value parameter is null, the record is not updated, and an error is not thrown https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_GlideRecord-setValue_String_Object