Adding incident numbers into JSON object

Asmita7
Tera Expert

Hello friends,

 

I have a requirement.

 

I want to add incident numbers arise from a query into JSON objects. It may be 10 or 15 0r 20 incidents retrieved from query.

 

Eg.

var gr = new GlideRecord('incident');
gr.addQuery('category','software');
gr.query();
while(gr.next()){
//  I want to add these incident numbers to a JSON object
}
 
Here I want to print the JSON object.
 
Please help me with the apporach.
 
Thank you,
Asmi
 
 
5 ACCEPTED SOLUTIONS

Brad Bowman
Kilo Patron
Kilo Patron

Here's one method for creating an array of JSON objects from GlideRecord results:

var objArr = [];
var gr = new GlideRecord('incident');
gr.addQuery('category','software');
gr.query();
while (gr.next()) {
	var obj = {};
	obj.number = gr.number.toString();
	objArr.push(JSON.stringify(obj)); 
}

gs.print(objArr.join(','));

 

View solution in original post

Juhi Poddar
Kilo Patron

Hello @Asmita7 

You can try this:

var incidents = [];
var incidentData = {};
var gr = new GlideRecord('incident');
gr.addQuery('category','software');
gr.query();
// Loop through the records and add incident numbers to the array
while(gr.next()) {
    incidentData = {
        'incident_number': gr.getDisplayValue('number')  
    };
    incidents.push(incidentData);  // Add each incident data object to the array
}

// Print the JSON object containing the incidents
var jsonString = JSON.stringify(incidents);  
gs.info(jsonString); 
gs.info(JSON.parse(jsonString)[0].incident_number);  // Logs the JSON string in the system logs

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

Sandeep Rajput
Tera Patron
Tera Patron

@Asmita7 You can try the following script.

 

var gr = new GlideRecord('incident');
gr.addQuery('category','software');
gr.query();
var jsonArray = [];
while(gr.next()){
var jsonObj = {};
jsonObj["number"] = gr.getValue('number');
jsonArray.push(jsonObj);
}
gs.info(JSON.stringify(jsonArray));

Hope this helps.

View solution in original post

PritamG
Mega Guru

 

var gr = new GlideRecord('incident');
gr.addQuery('category', 'software');
gr.query();

var incidents = []; // Array to hold incident numbers
while (gr.next()) {
    incidents.push(gr.number.toString()); // Add incident number to the array
}

var jsonObject = { incidents: incidents }; // Create JSON object
gs.print(JSON.stringify(jsonObject)); // Print JSON object

 

This will output a JSON object with all incident numbers.

 

View solution in original post

Viraj Hudlikar
Giga Sage

Hello @Asmita7 

 

var grINC = new GlideRecord('incident');
grINC .addQuery('category', 'software');
grINC .addActiveQuery();
grINC .query();

var incidents = [];
while (grINC .next()) {
    incidents.push({
        number: grINC .getValue('number'),
        caller: grINC .getDisplayValue('caller_id'),
        assigned_to: grINC .getDisplayValue('assigned_to')
    });
}

var jsonObject = {
    incidents: incidents
};

// Convert the JSON object to a string and beautify it
var beautifiedJson = JSON.stringify(jsonObject, null, 4);
gs.print(beautifiedJson);

 

 

When above code is run in background script output is as below:

VirajHudlikar_0-1737810254593.png

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

 

View solution in original post

8 REPLIES 8

Brad Bowman
Kilo Patron
Kilo Patron

Here's one method for creating an array of JSON objects from GlideRecord results:

var objArr = [];
var gr = new GlideRecord('incident');
gr.addQuery('category','software');
gr.query();
while (gr.next()) {
	var obj = {};
	obj.number = gr.number.toString();
	objArr.push(JSON.stringify(obj)); 
}

gs.print(objArr.join(','));

 

Juhi Poddar
Kilo Patron

Hello @Asmita7 

You can try this:

var incidents = [];
var incidentData = {};
var gr = new GlideRecord('incident');
gr.addQuery('category','software');
gr.query();
// Loop through the records and add incident numbers to the array
while(gr.next()) {
    incidentData = {
        'incident_number': gr.getDisplayValue('number')  
    };
    incidents.push(incidentData);  // Add each incident data object to the array
}

// Print the JSON object containing the incidents
var jsonString = JSON.stringify(incidents);  
gs.info(jsonString); 
gs.info(JSON.parse(jsonString)[0].incident_number);  // Logs the JSON string in the system logs

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Gangadhar Ravi
Giga Sage
Giga Sage

@Asmita7 you can try something like below.

 

var incidentNumbers = [];
var gr = new GlideRecord('incident');
gr.addQuery('category', 'software'); 
gr.query();
while (gr.next()) {
    incidentNumbers.push(gr.getValue('number'));
}
var jsonObject = {
    incidents: incidentNumbers
};
var jsonString = JSON.stringify(jsonObject, null, 2);
gs.print(jsonString);

Sandeep Rajput
Tera Patron
Tera Patron

@Asmita7 You can try the following script.

 

var gr = new GlideRecord('incident');
gr.addQuery('category','software');
gr.query();
var jsonArray = [];
while(gr.next()){
var jsonObj = {};
jsonObj["number"] = gr.getValue('number');
jsonArray.push(jsonObj);
}
gs.info(JSON.stringify(jsonArray));

Hope this helps.