JSON APIs
Summarize
Summary of JSON APIs
JSON APIs in ServiceNow enable you to process and encrypt JSON payload data by interacting with JSON content obtained via thegetAsJsonContent()method on a request or ParameterValue object. This capability is essential for securely handling data by mapping JSON elements to ServiceNow table fields and applying encryption rules dynamically during data ingestion or processing.
Show less
Key Features
- JSON Content Access: Use
getAsJsonContent()to retrieve an iterable JSON object (JsonNode), allowing traversal of JSON data. - Iterators: Obtain a
JsonNodeIteratorviagetIterator()oriterator()methods to iterate over JSON nodes safely usinghasNext()andnext()methods. - Field Mapping and Encryption: Map JSON elements to specific table fields using
valueFor(tableName, fieldName). This instructs the proxy to check encryption requirements and encrypt field values if configured. - Dynamic Handling of Unknown Structures: You can iterate over JSON arrays and objects even when the exact schema is unknown, dynamically mapping fields and applying encryption rules based on runtime information such as URL parameters.
- Query Encryption Support: Use
encodedQueryFor(tableName)to encrypt values within encoded query strings found in JSON payloads.
Practical Use Cases
- Mapping Known JSON Fields: For example, encrypting the
descriptionnodes within a JSON payload and storing them in theshortdescriptionfield of theincidenttable. - Handling Variable JSON Structures: When processing JSON where field names correspond to table columns but are not fixed, iterating over each record and field allows for conditional encryption based on the table and field names determined at runtime.
- Encrypting Queries: Encrypting sensitive data within query parameters embedded in JSON payloads before processing them on the instance.
Key Outcomes for ServiceNow Customers
- Ability to securely ingest and process JSON data with encryption applied consistently according to configured encryption rules.
- Flexible JSON processing that supports both structured and dynamic payloads without loss of data security.
- Improved data protection by automatically encrypting sensitive fields during integrations or API data handling.
- Streamlined development of encryption rules using JSON API methods that integrate seamlessly with existing ServiceNow encryption configurations.
JSON APIs can be used after calling getAsJsonContent() on either the request object or a ParameterValue property.
- Call getAsJsonContent() on the request object. This returns an iterable object of the JsonNode underlying class.
- Call iterator() or getIterator(String xPath) on the JsonNode object. This returns a JsonNodeIterator object that can be used to iterate over nodes in the JSON object.
- Call the hasNext() method on the JsonNodeIterator object to determine whether another element is available.
- Call next() on the JsonNodeIterator object to return the next JSON element. You cannot call next() without first calling hasNext().
- Call valueFor(String tableName, String fieldName) on the
JSON element. This method tells the proxy that the value for this element
maps to the specified field in the specified table. The proxy then checks
whether the field must be encrypted.Note:To determine if you want to call valueFor(String tableName, String fieldName) on a JSON element, you can use the getName() method to return the name of the element.
Mapping to a known table-field on the instance
In this example, the JSON payload is processed on the instance to insert records in the incident table. The description field populates short_description on the incident.
{
data: {
records: [
{
"name": "Test Record 1",
"description": "Test Record 1 Description",
"tag": "security"
},
{
"name": "Test Record 1",
"description": "Test Record 1 Description",
"tag": "security"
}],
"query": "assigned_to=3D4860165813e63a00d00abd322244b092^category=vulnerability"
},
"source": "10.11.13.14"
}The following rule can apply:
function sampleJsonAction1() {
var jsonContent = request.getAsJsonContent();
// This loop iterates over all description elements in the records array
var jsonNodeIterator = jsonContent.getIterator(’/data/records/description’);
while (jsonNodeIterator.hasNext()) {
var jsonNode = jsonNodeIterator.next();
jsonNode.valueFor('incident', 'short_decription');
}
}
This action iterates through the description nodes and asks the proxy server to encrypt the values and insert them into incident.short_description on the instance.
Mapping to an unknown table-field on the instance
In this example, the rule iterates over records, but is not sure what nodes to expect. The only known is that for each object within records, the nodes match the names of the columns specified in the table URL parameter.
The rule also specifies that, if the table is incident, then the data in the description node should be encrypted and stored in the short_description field on the instance.
function sampleJsonAction2() {
var jsonContent = request.getAsJsonContent();
var tableName = request.urlParam.table;
// This first iterator will iterate over all record elements
var jsonNodeIterator = jsonContent.getIterator('data/records');
while (jsonNodeIterator.hasNext()) {
encryptFieldsInRecord(jsonNodeIterator.next());
}
}
function encryptFieldsInRecord(jsonNode) {
//this time we want to iterate over all nodes
var fieldIterator = jsonNode.iterator();
while (fieldIterator.hasNext()) {
var field = fieldIterator.next();
var fieldname = childElement.getName();
if (fieldName == 'description') {
field.valueFor(tableName, 'short_description');
} else {
field.valueFor(tableName, fieldName);
}
}
}In the encryptFieldsInRecord() function, the valueFor() method is called on a table and a field that are dynamically assigned based on the request. Even though the table and field names can change, the rule asks the proxy to check whether the field in the table must be encrypted based on the encryption configurations defined.
If the field is not configured for encryption, or if the node name does not match a field in the table, the proxy skips that node. If the node name matches a field marked for encryption, then the proxy encrypts the value.
Using an encoded query
function sampleJsonAction3() {
var jsonContent = request.getAsJsonContent();
var tableName = request.urlParam.table;
// This first iterator will iterate over all record elements
var jsonNodeIterator = jsonContent.getIterator('data');
while (jsonNodeIterator.hasNext()) {
var jsonNode = jsonNodeIterator.next();
if (jsonNode.getName() == 'records')
encryptRecors(jsonNodeIterator.next());
else if (jsonNode.getName() == 'query')
jsonNode.encodedQueryFor(tableName);
}
}
function encryptRecords(jsonNode) {
//we iterate over all fields in the node
var recordIterator = jsonNode.iterator();
while (recordIterator.hasNext()) {
encryptFieldsInRecord(recordIterator.next());
}
}
function encryptFieldsInRecord(jsonNode) {
//this time we want to iterate over all nodes
var fieldIterator = jsonNode.iterator();
while (fieldIterator.hasNext()) {
var field = fieldIterator.next();
var fieldname = childElement.getName();
field.valueFor(tableName, fieldName);
}
}In this example, the rule iterates over data. As it finds records, it performs the same logic as in the second example, iterating over fields in each node. When it finds the query node, it calls encodedQueryFor() to encrypt values that should be encrypted in the query.