Parse JSON/Payload

Vamshi_ch123
Tera Contributor

Hi @Pavankumar_1 

 

Payload:

{"receiver":"","status":"firing","alerts":[{"status":"firing","labels":{"alertname":"TestAlert","instance":"Grafana"},"annotations":{"summary":"Notification test"},"startsAt":"2023-06-20T10:19:31.207102004Z","endsAt":"0001-01-01T00:00:00Z","generatorURL":"","fingerprint":" ","silenceURL":"httpence/new?alertmanager=grafana\u0026matcher=alertname%3DTestAlert\u0026matcher=instance%3DGrafana","dashboardURL":"","panelURL":"","values":null,"valueString":"[ metric='foo' labels={instance=bar} value=10 ]"}],"groupLabels":{},"commonLabels":{"alertname":"TestAlert","instance":"Grafana"},"commonAnnotations":{"summary":"Notification test"},"externalURL":"https://sao/","version":"1","groupKey":"{alertname=\"TestAlert\", instance=\"Grafana\"}2023-06-20 10:19:31.207102004 +0000 UTC m=+6508.07","truncatedAlerts":0,"orgId":1,"title":"[FIRING:1] (TestAlert Grafana)","state":"alerting","message":"**Firing**\n\nValue: [no value]\nLabels:\n - alertname = TestAlert\n - instance = Grafana\nAnnotations:\n - summary = Notification test\nSilence: h?alertmanager=grafana\u0026matcher=alertname%3DTestAlert\u0026matcher=instance%3DGrafana\n"}

 

Issue to parse the values using scripted rest resource from above payload

script:

 

var reqbody = request.body.dataString;
    var parser = new global.JSON();
   var parsedData = JSON.parse(reqbody);
    
    gs.info("Grafana : REQ " + reqbody);
    var Source = parsedData.instance;
    var Node = parsedData.state;
    var Description = parsedData.summary;
    var Additional_Info = parsedData.alertname;
    var Source_Instance = parsedData.summary;
 
    

    var gr = new GlideRecord("em_event");
    gr.initialize();
    gr.source = Source;
    gr.node = Node;
    gr.description = Description;
    gr.additional_info = Additional_Info;
    gr.event_class = Source_Instance;
    gr.insert();
 
when I create a test ticket the values are blank for the fields in the events table except node.
 
Thank you
1 ACCEPTED SOLUTION

Pavankumar_1
Mega Patron

Hi @Vamshi_ch123 ,

you can replace the below code. we need to get the values like below and use.

var reqbody = request.body.dataString;
var parser = new global.JSON();
var parsedData = JSON.parse(reqbody);
gs.info("Grafana : REQ " + reqbody);
var Source = parsedData.commonLabels.instance;
var Node = parsedData.state;
var Description = parsedData.alerts[0].annotations.summary;//not sure which summary you need but you can the summary of commonAnnotations also
var Additional_Info = parsedData.commonLabels.alertname;
var Source_Instance = parsedData.alerts[0].annotations.summary;

  

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

4 REPLIES 4

Pavankumar_1
Mega Patron

Hi @Vamshi_ch123 ,

you can replace the below code. we need to get the values like below and use.

var reqbody = request.body.dataString;
var parser = new global.JSON();
var parsedData = JSON.parse(reqbody);
gs.info("Grafana : REQ " + reqbody);
var Source = parsedData.commonLabels.instance;
var Node = parsedData.state;
var Description = parsedData.alerts[0].annotations.summary;//not sure which summary you need but you can the summary of commonAnnotations also
var Additional_Info = parsedData.commonLabels.alertname;
var Source_Instance = parsedData.alerts[0].annotations.summary;

  

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Hi @Pavankumar_1  

 

from the payload can we update the URL to the string field?

{

"receiver":"Servicenow DEV",

"status":"firing",

"alerts":[

{

"status":"resolved",

"labels":{

"Customer":"SS",

"Customer_Code":"XX",

"EventType":"Application",

"Severity":"Critical",

"alertname":"Response Time (90pct)",

"grafana_folder":"XP -S"

},

"annotations":{

"grafana_state_reason":"Updated"

},

"silenceURL":"https://sae/new?alertmanager=grafana\u0026matcication\u0026matatcher=alertname%3DApplicatio890pct%29\u0026matcher=",

"dashboardURL":"https://saro/d/JDyyBMQ4z?orgId=1",

"panelURL":"https://orgId=1\u0026viewPanel=44",

"values":null,"valueString":"[ var='B' labels={} value=null ], [ var='C' labels={} value=null ]"

},

From the payload I was trying to update the dashboard URL to the description but it is showing undefined

var Description = parsedData.alerts[0].annotations.dashboardURL;

Hi @Vamshi_ch123 ,

use as below

var Description = parsedData.alerts[0].dashboardURL;
If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar