Understanding Key Value Pairs In A JSON Object In ServiceNow

What Are Key Value Pairs In JSON? In the realm of ServiceNow scripting, understanding how to navigate key-value pairs in an object is fundamental. Why? Because they form the bedrock of data organization in objects, …

Buy The "ServiceNow Developer's Manual" Now

We've packed over a decade of ServiceNow Development advice, into a single e-book.

Buy It Now
Stripe Climate Badge

The Snowball will contribute 5% of your purchase to remove CO₂ from the atmosphere.

What Are Key Value Pairs In JSON?

In the realm of ServiceNow scripting, understanding how to navigate key-value pairs in an object is fundamental. Why? Because they form the bedrock of data organization in objects, particularly JSON, making data accessible and manipulatable.

Key-value pairs serve as the primary data structure within JavaScript Object Notation (JSON), which is frequently used in ServiceNow for data exchange between the server and web applications. A key-value pair consists of a unique identifier (key) associated with a specific value. This structure is crucial for efficient data retrieval and modification, allowing you to target and access the data you need directly using the key.

However, when working with key-value pairs, it’s important to know that keys are case-sensitive and must be unique within an object. Also, remember that the values associated with these keys can be of different types, including strings, numbers, arrays, boolean, null, or even other objects.

Using Coding Examples With Objects

Now, let’s take a look at a real-world scenario. Suppose you are working with incident data stored as a JSON object, like so:

var incidentData = {
    "number": "INC0010001",
    "state": "New",
    "priority": "1",
    "assigned_to": "John Doe"

To retrieve data, you would use the key associated with the value you want to access:

gs.info(incidentData.number);  // Outputs: "INC0010001"

If you wanted to change the state of the incident, you would assign a new value to the corresponding key:

incidentData.state = "In Progress";
gs.info(incidentData.state);  // Outputs: "In Progress"

Moreover, sometimes you might have to navigate complex JSON objects with nested key-value pairs. In such cases, you need to specify the complete path to the key:

var complexData = {
    "incident": {
        "details": {
            "number": "INC0010001",
            "state": "New"
        "assignment": {
            "group": "Service Desk",
            "individual": "John Doe"

gs.info(complexData.incident.details.number);  // Outputs: "INC0010001"

What Is Bracket Notation In Objects?

In JavaScript, which is the scripting language used by ServiceNow, there are two ways to retrieve values from an object – dot notation and bracket notation. While dot notation is more commonly used, bracket notation provides some flexibility that can be useful in certain situations.

Bracket notation is particularly handy when the property name is stored in a variable or the property name is a number or contains spaces or special characters. It’s also required if the property name is computed dynamically.

Here’s how you can use bracket notation in ServiceNow to retrieve values from an object:

Suppose you have an object that represents an incident in ServiceNow:

var incident = {
    number: 'INC12345',
    state: 'New',
    priority: 1,
    assigned_to: 'John Doe'

You can access the values of this object’s properties using bracket notation as follows:

gs.info(incident['number']);  // Outputs: 'INC12345'
gs.info(incident['state']);  // Outputs: 'New'
gs.info(incident['priority']);  // Outputs: 1
gs.info(incident['assigned_to']);  // Outputs: 'John Doe'

If the property name is stored in a variable, you can use that variable inside the brackets:

var prop = 'state';
gs.info(incident[prop]);  // Outputs: 'New'

Please note that while dot notation is quicker to write and read, bracket notation allows access to property names that would otherwise be impossible with dot notation. Both have their uses in ServiceNow scripting, and understanding when to use which can make your scripts more flexible and powerful.

Understanding key-value pairs in JSON significantly enhances your ability to work with ServiceNow data. By enabling precise targeting of data, these pairs empower developers and administrators to efficiently read, manipulate, and organize data within the platform.

As you continue your ServiceNow journey, leveraging key-value pairs will be instrumental in developing robust and efficient scripts.

Remember, patience and practice are the keys to mastering any skill.

Happy scripting!

What Do You Think About This Article?

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Product Image

Amy just purchased A ServiceNow Developer’s Manual 23 Hours ago from Baltimore MD.

Buy Now
Would love your thoughts, please comment.x