Mastering GlideJSONPath in ServiceNow: A Practical Guide with Examples
Image courtesy: https://www.opc-router.com/what-is-json/

Mastering GlideJSONPath in ServiceNow: A Practical Guide with Examples

Introduction:

ServiceNow developers often encounter JSON data within their applications, whether it's retrieved from external APIs, stored in ServiceNow tables, or passed between client and server scripts. GlideJSONPath is a powerful API in the ServiceNow platform that simplifies the manipulation and extraction of JSON data. In this article, we will explore GlideJSONPath's features through practical examples, demonstrating how it can be effectively utilized within ServiceNow applications.

Understanding GlideJSONPath:

GlideJSONPath is a JavaScript library that provides a concise syntax for querying and manipulating JSON data. It enables developers to traverse complex JSON structures with ease, perform filtering, transformation, and extract specific data elements efficiently.

Example 1: Retrieving Data from an External API

Suppose we have an integration with a weather API that returns JSON data containing weather forecasts for different cities. We want to extract the temperature forecast for a specific city.

```javascript

// JSON Response from Weather API

var jsonResponse = {

"city": "New York",

"forecast": [

{"date": "2024-02-18", "temperature": 24},

{"date": "2024-02-19", "temperature": 26},

{"date": "2024-02-20", "temperature": 23}

]

};

// GlideJSONPath Expression to Extract Temperature for New York

var temperature = new GlideJSONPath(jsonResponse).query("$.forecast[?(@.date=='2024-02-18')].temperature")[0];

gs.info("Temperature in New York on 2024-02-18: " + temperature);

```

Example 2: Dynamic Form Field Population

Suppose we have a ServiceNow catalog item form where users select a product category, and based on their selection, additional fields need to be populated dynamically.

Javascript

// JSON Data Mapping Product Categories to Additional Fields

var categoryMappings = {

"Laptop": {"RAM": "16GB", "Storage": "512GB SSD"},

"Smartphone": {"OS": "Android", "Camera": "12MP"},

"Tablet": {"Screen Size": "10 inches", "Battery Life": "10 hours"}

};

// GlideJSONPath Expression to Retrieve Additional Fields Based on Selected Category

var selectedCategory = current.variables.product_category.toString();

var additionalFields = new GlideJSONPath(categoryMappings).query("$." + selectedCategory);

// Populate Additional Fields on the Form

for (var field in additionalFields) {

if (additionalFields.hasOwnProperty(field)) {

current.variables[field] = additionalFields[field];

}

}

```

Example 3: Data Transformation for Reporting

Suppose we have JSON data representing sales transactions and we want to aggregate the total sales amount for each product category.

```javascript

// JSON Data representing Sales Transactions

var salesData = [

{"product": "Laptop", "amount": 1500},

{"product": "Smartphone", "amount": 800},

{"product": "Tablet", "amount": 600},

{"product": "Laptop", "amount": 1600},

{"product": "Smartphone", "amount": 750}

];

// GlideJSONPath Expression to Aggregate Total Sales Amount by Product Category

var aggregatedData = new GlideJSONPath(salesData).query("$..['product','amount']");

var salesByCategory = {};

aggregatedData.forEach(function(item) {

var product = item.product;

var amount = item.amount;

if (!salesByCategory[product]) {

salesByCategory[product] = 0;

}

salesByCategory[product] += amount;

});

// Output Total Sales Amount by Product Category

for (var category in salesByCategory) {

if (salesByCategory.hasOwnProperty(category)) {

gs.info("Total Sales Amount for " + category + ": $" + salesByCategory[category]);

}

}

Conclusion:

GlideJSONPath is a versatile API in the ServiceNow platform, empowering developers to efficiently manipulate and extract JSON data within their applications. By mastering GlideJSONPath's features and incorporating it into ServiceNow development practices, developers can streamline workflows, enhance user experiences, and unlock the full potential of their digital workflows. With the examples provided, developers can begin leveraging GlideJSONPath effectively in their ServiceNow applications.

Frederick Leduc

Consultant - Program ITSM specialist with multiple roles (Architect, Delivery Application Manager, Business Analyst, Functional Analyst, Development Coordinator, QA Coordinator) - ServiceNow, Micro-Focus, Maximo, Remedy

1 年

Thierry Gribeauval est-ce que ?a aurait aidé à l’occasion avec ce qu’on a fait ensemble pour Compucom ;) ? Julien a eu du gros fun avec aussi avec le parsing des array avec Bell

Is there any benefit in using the API instead of using the plain object notation?

要查看或添加评论,请登录

Mohit Gupta的更多文章

社区洞察

其他会员也浏览了