- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
📦 Working with JSON in ServiceNow Scripts
I keep a few useful code snippets in a fix script on my personal instance for reference, and I find myself returning to them frequently. One technique I've been using more and more is organizing data using JSON objects rather than multi-dimensional arrays. JSON makes data more readable, maintainable, and easier to work with when accessing nested values.
🔧 Creating a JSON Object
To begin, we instantiate a new JavaScript object using curly brace {} notation:
var jsnObj = {}; // Instantiate a new object
🔍 Querying the Incident Table
Next, let’s use a simple GlideRecord query to fetch data from the incident table. In this example, we’re retrieving a limited set of active incidents that have an assignment group.
var grInc = new GlideRecord('incident');
grInc.setLimit(3); // Limit to 3 records for testing
grInc.addQuery('active', true);
grInc.addNotNullQuery('assignment_group');
grInc.query();
Let’s initially inspect the results using a simple if (grInc.next()) structure to make the output easier to read:
if (grInc.next()) {
var sys = grInc.getValue('sys_id');
var number = grInc.getValue('number');
var priority = grInc.getValue('priority');
var state = grInc.getValue('state');
var group = grInc.getDisplayValue('assignment_group');
var groupManager = grInc.getDisplayValue('assignment_group.manager');
gs.info('Sys ID: ' + sys);
gs.info('Number: ' + number);
gs.info('Priority: ' + priority);
gs.info('State: ' + state);
gs.info('Group: ' + group);
gs.info('Group Manager: ' + groupManager);
}
Output:
*** Script: Sys ID: 01c01200d7002100b81145a3ce610389
*** Script: Number: INC0006831
*** Script: Priority: 5
*** Script: State: 2
*** Script: Group: Financial Systems Support
*** Script: Group Manager: Jacinto Gawron
🧱 Building the JSON Object
To organize this data into a structured JSON object, let’s switch from if to while, and build the object with nested members for each record:
var jsnObj = {}; // Instantiate a new object
while (grInc.next()) {
var sys = grInc.getValue('sys_id');
var number = grInc.getValue('number');
var priority = grInc.getValue('priority');
var state = grInc.getValue('state');
var group = grInc.getDisplayValue('assignment_group');
var groupManager = grInc.getDisplayValue('assignment_group.manager');
jsnObj[sys] = {
number: number,
priority: priority,
state: state,
group: {
name: group,
manager: groupManager
}
};
}
We're using each incident's sys_id as a unique key in the top-level object. Inside each, we add fields like number, priority, and a nested group object containing the assignment group name and manager.
Finally, we output the full JSON structure in a pretty-printed format:
gs.info(JSON.stringify(jsnObj, null, 2));
🧾 Example Output
{
"01c01200d7002100b81145a3ce610389": {
"number": "INC0006831",
"priority": "5",
"state": "2",
"group": {
"name": "Financial Systems Support",
"manager": "Jacinto Gawron"
}
},
"03a05a22dba3155003ef43ea1396192c": {
"number": "INC0402390",
"priority": "5",
"state": "1",
"group": {
"name": "Software",
"manager": "Melinda Carleton"
}
},
"04407ac165feef00964fc62d7eab1e36": {
"number": "INC0010051",
"priority": "5",
"state": "1",
"group": {
"name": "",
"manager": ""
}
}
}
🧩 Use Case: Generate a Summary Report by Manager
Once you’ve built your jsnObj
, you can process it to count how many incidents are assigned to each group manager, as an example. I wanted to show how you would access the data, as that's the whole point of this, right?
var incidentSummary = {};
for (var sysId in jsnObj) {
var manager = jsnObj[sysId]["group"]["manager"];
if (!incidentSummary[manager]) {
incidentSummary[manager] = 0;
}
incidentSummary[manager]++;
}
// Output the summary
for (var mgr in incidentSummary) {
if (!gs.nil(mgr)){
gs.info(mgr + ' has ' + incidentSummary[mgr] + ' incident(s).');
} else {
gs.info(incidentSummary[mgr] + ' incident(s) have no manager for their assignment group.');
}
}
This will give us the following results:
*** Script: Jacinto Gawron has 1 incident(s).
*** Script: Melinda Carleton has 1 incident(s).
*** Script: 1 incident(s) have no manager for their assignment group.
💡 Why JSON?
This approach makes it incredibly easy to access data by key, structure nested information cleanly, and even export to external systems if needed. Compared to multi-dimensional arrays, this format is more readable and scalable — especially when dealing with real-world data sets that can get very complex.
Full Script
var grInc = new GlideRecord('incident');
grInc.setLimit(3); // Limit to 3 records for testing
grInc.addQuery('active', true);
grInc.addNotNullQuery('assignment_group');
grInc.query();
var jsnObj = {}; // Instantiate a new object
while (grInc.next()) {
var sys = grInc.getValue('sys_id');
var number = grInc.getValue('number');
var priority = grInc.getValue('priority');
var state = grInc.getValue('state');
var group = grInc.getDisplayValue('assignment_group');
var groupManager = grInc.getDisplayValue('assignment_group.manager');
jsnObj[sys] = {
number: number,
priority: priority,
state: state,
group: {
name: group,
manager: groupManager
}
};
}
//gs.info(JSON.stringify(jsnObj, null, 2));
var incidentSummary = {}; // key = manager name, value = count
for (var sysId in jsnObj) {
var manager = jsnObj[sysId]["group"]["manager"];
if (!incidentSummary[manager]) {
incidentSummary[manager] = 0;
}
incidentSummary[manager]++;
}
// Output the summary
for (var mgr in incidentSummary) {
if (!gs.nil(mgr)){
gs.info(mgr + ' has ' + incidentSummary[mgr] + ' incident(s).');
} else {
gs.info(incidentSummary[mgr] + ' incident(s) have no manager for their assignment group.');
}
}
- 1,931 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.