Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Convert Active Incidents to JSON – ServiceNow
Overview

This script fetches all active Incident records from your ServiceNow instance and converts them into a JSON format. The JSON output is easy to read and can be used for reporting, integration, or debugging purposes.

By converting records to JSON, you can quickly share structured data with external systems, automate processes, or use it in scripts and dashboards.

Features

Retrieves all active incidents from the incident table.

Dynamically extracts selected fields (configurable).

Automatically resolves reference fields to display values.

Adds human-readable state labels (e.g., "New", "In Progress", "Resolved").

Outputs pretty-printed JSON for easy readability.

Configuration

Table Name: Set the tableName variable to the table you want to extract records from.

Fields to Include: Update the fieldsToInclude array to include the fields you need in the JSON. For example:
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Table and fields you want to include
var tableName = 'incident'; // Change to any table you like
var fieldsToInclude = ['number', 'short_description', 'state', 'assigned_to', 'sys_created_on'];// Change whichever field you wish for

// Store all incident data here
var incidentList = [];

// Get active incidents
var gr = new GlideRecord(tableName);
gr.addQuery('active', true);
gr.query();

// Go through each record and build a friendly object
while (gr.next()) {
var incidentObj = {};

fieldsToInclude.forEach(function(field) {
if (gr.isValidField(field) && gr[field].getRefRecord) {
// Get display value for reference fields
incidentObj[field] = gr[field].getDisplayValue();
} else if (gr.isValidField(field)) {
incidentObj[field] = gr[field].toString();
} else {
incidentObj[field] = null;
}
});

// Add human-readable state
var stateMap = {
'1': 'New',
'2': 'In Progress',
'3': 'On Hold',
'6': 'Resolved',
'7': 'Closed'
};
incidentObj.state_label = stateMap[incidentObj.state] || 'Unknown';

incidentList.push(incidentObj);
}

// Convert the list to JSON in a readable format
var jsonOutput = JSON.stringify(incidentList, null, 2);

// Show the JSON output in system logs
gs.info("Here’s your JSON for active incidents:\n" + jsonOutput);
Loading