Skip to content

Commit 85f20b7

Browse files
authored
Add GlideJsonPath Example to Core ServiceNow APIs (#1828)
* add GlideJsonPath Example * move to subcategory * formatting reference list
1 parent f49d864 commit 85f20b7

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Querying JSON with JSONPath to extract values
2+
3+
GlideJsonPath is a class which can be used to use JSONPath in ServiceNow. It can be useful when working with JSON Payloads, especially highly nested or in general complex structures.
4+
5+
References:
6+
- [RFC 9535: JSONPath: Query Expressions for JSON](https://datatracker.ietf.org/doc/rfc9535/)
7+
- [Play with JSONPath outside of ServiceNow](https://jsonpath.com/)
8+
- [Good Examples to start with](https://restfulapi.net/json-jsonpath/)
9+
- [ServiceNow API Documentation](https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/GlideJsonPath/concept/GlideJsonPathAPI.html)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Run in background script
2+
var json = {
3+
"store":
4+
{
5+
"book": [
6+
{
7+
"category": "reference",
8+
"author": "Nigel Rees",
9+
"title": "Sayings of the Century",
10+
"price": 8.95
11+
},
12+
{
13+
"category": "fiction",
14+
"author": "Evelyn Waugh",
15+
"title": "Sword of Honour",
16+
"price": 12.99
17+
},
18+
{
19+
"category": "fiction",
20+
"author": "Herman Melville",
21+
"title": "Moby Dick",
22+
"isbn": "0-553-21311-3",
23+
"price": 8.99
24+
},
25+
{
26+
"category": "fiction",
27+
"author": "J. R. R. Tolkien",
28+
"title": "The Lord of the Rings",
29+
"isbn": "0-395-19395-8",
30+
"price": 22.99
31+
}
32+
],
33+
"bicycle": {
34+
"color": "red",
35+
"price": 19.95
36+
}
37+
}
38+
};
39+
40+
var path1 = 'store.book[0].author'; // The author of the first book
41+
var path2 = 'store.book[*].author'; // All authors
42+
var path3 = 'store..price'; // All prices
43+
var path4 = '$..book[?(@.price<10)]'; // All books cheaper than 10
44+
var path5 = '$..book[?(@.isbn)]'; // All books with an ISBN number
45+
var path6 = '$..*'; // All members of JSON structure
46+
47+
var gjp = new GlideJsonPath(JSON.stringify(json));
48+
gs.info('Path: ' + path1 + ' Result: ' + gjp.read(path1));
49+
gs.info('Path: ' + path2 + ' Result: ' + gjp.read(path2));
50+
gs.info('Path: ' + path3 + ' Result: ' + gjp.read(path3));
51+
gs.info('Path: ' + path4 + ' Result: ' + gjp.read(path4));
52+
gs.info('Path: ' + path5 + ' Result: ' + gjp.read(path5));
53+
gs.info('Path: ' + path6 + ' Result: ' + gjp.read(path6));

0 commit comments

Comments
 (0)