- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2025 10:54 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2025 12:20 PM
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(','));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2025 01:33 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2025 06:44 PM
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2025 08:33 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-25-2025 05:04 AM - edited ‎01-25-2025 05:07 AM
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:
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2025 12:20 PM
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(','));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2025 01:33 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2025 05:49 PM
@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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2025 06:44 PM
@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.