How to print the out in JSON format

Peter Wood
Kilo Guru

I want to print the incident numbers per assignment group in JSON format.

 

Eg: {"Group1" : [INC1,INC2,INC3],
"Group2" : [INC4,INC5,INC6]
}

1 ACCEPTED SOLUTION

ayan_murshad
Tera Guru

Hello @Peter Wood 


Try below script

var inc = new GlideRecord('incident');
inc.addEncodedQuery('active=true^short_description=Test');
inc.query();

var incidents = [];
while(inc.next()){
incidents.push({'incident_number': inc.getValue('number'),'assignment_group': inc.getValue('assignment_group')});
}
var groupedIncidents = {};
incidents.forEach(function (incident) {
var group = incident.assignment_group;
var number = incident.incident_number;

if (groupedIncidents[group]) {
groupedIncidents[group].push(number);
} else {
groupedIncidents[group] = [number];
}
});
var jsonResult = JSON.stringify(groupedIncidents);
gs.log(jsonResult);

 
Regards

Ayan Murshad 


View solution in original post

1 REPLY 1

ayan_murshad
Tera Guru

Hello @Peter Wood 


Try below script

var inc = new GlideRecord('incident');
inc.addEncodedQuery('active=true^short_description=Test');
inc.query();

var incidents = [];
while(inc.next()){
incidents.push({'incident_number': inc.getValue('number'),'assignment_group': inc.getValue('assignment_group')});
}
var groupedIncidents = {};
incidents.forEach(function (incident) {
var group = incident.assignment_group;
var number = incident.incident_number;

if (groupedIncidents[group]) {
groupedIncidents[group].push(number);
} else {
groupedIncidents[group] = [number];
}
});
var jsonResult = JSON.stringify(groupedIncidents);
gs.log(jsonResult);

 
Regards

Ayan Murshad