
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 02-18-2024 10:51 AM
GlideJsonPath API
I like to work with JSON, ServiceNow in Vancouver has introduced a new API related to JSON which is **GlideJsonPath.**
I find this API very useful as it helps reduce the amount of code needed when working with JSON.
I have referred to many different sources and discovered a couple of useful features. Here are a few of the use cases.
$ | root element |
* | wild card |
[,] | array |
?(@) |
script with the current object |
var js = {
"task": {
"incident": [
{
"number": "INC12345",
"state": "open"
},
{
"number": "INC12346",
"state": "in progress"
},
{
"number": "INC12347",
"state": "in progress"
}
],
"user": {
"active": true
}
}
}
1. Use a dot to travel in the path of JSON
var gjp = new GlideJsonPath(JSON.stringify(js));
var numbers = gjp.read("$.task.*");
gs.print(numbers)
gs.print(' Type ' + Object.prototype.toString.call(numbers));
output
2. Getting specific items from the array
var gjp = new GlideJsonPath(JSON.stringify(js));
var numbers = gjp.read("$.task.incident[0,1]");
gs.print(numbers)
gs.print(' Type ' + Object.prototype.toString.call(numbers));
output
3. Using a script to filter the only open state incident
var gjp = new GlideJsonPath(JSON.stringify(js));
var numbers = gjp.read("$.task.incident[?(@.state=='open')]");
gs.print(numbers)
gs.print(' Type ' + Object.prototype.toString.call(numbers));
output
These are a couple of examples which might be useful when interacting with JSON. There are many more use cases, and we will try to keep this article updated. Don't forget to drop in and check for updates.
Thanks
Murali
- 765 Views