diff --git a/Core ServiceNow APIs/GlideJsonPath/Basic-Example/README.md b/Core ServiceNow APIs/GlideJsonPath/Basic-Example/README.md new file mode 100644 index 0000000000..c2a062042b --- /dev/null +++ b/Core ServiceNow APIs/GlideJsonPath/Basic-Example/README.md @@ -0,0 +1,9 @@ +# Querying JSON with JSONPath to extract values + +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. + +References: +- [RFC 9535: JSONPath: Query Expressions for JSON](https://datatracker.ietf.org/doc/rfc9535/) +- [Play with JSONPath outside of ServiceNow](https://jsonpath.com/) +- [Good Examples to start with](https://restfulapi.net/json-jsonpath/) +- [ServiceNow API Documentation](https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/GlideJsonPath/concept/GlideJsonPathAPI.html) \ No newline at end of file diff --git a/Core ServiceNow APIs/GlideJsonPath/Basic-Example/examples.js b/Core ServiceNow APIs/GlideJsonPath/Basic-Example/examples.js new file mode 100644 index 0000000000..03740bb770 --- /dev/null +++ b/Core ServiceNow APIs/GlideJsonPath/Basic-Example/examples.js @@ -0,0 +1,53 @@ +// Run in background script +var json = { + "store": + { + "book": [ + { + "category": "reference", + "author": "Nigel Rees", + "title": "Sayings of the Century", + "price": 8.95 + }, + { + "category": "fiction", + "author": "Evelyn Waugh", + "title": "Sword of Honour", + "price": 12.99 + }, + { + "category": "fiction", + "author": "Herman Melville", + "title": "Moby Dick", + "isbn": "0-553-21311-3", + "price": 8.99 + }, + { + "category": "fiction", + "author": "J. R. R. Tolkien", + "title": "The Lord of the Rings", + "isbn": "0-395-19395-8", + "price": 22.99 + } + ], + "bicycle": { + "color": "red", + "price": 19.95 + } + } +}; + +var path1 = 'store.book[0].author'; // The author of the first book +var path2 = 'store.book[*].author'; // All authors +var path3 = 'store..price'; // All prices +var path4 = '$..book[?(@.price<10)]'; // All books cheaper than 10 +var path5 = '$..book[?(@.isbn)]'; // All books with an ISBN number +var path6 = '$..*'; // All members of JSON structure + +var gjp = new GlideJsonPath(JSON.stringify(json)); +gs.info('Path: ' + path1 + ' Result: ' + gjp.read(path1)); +gs.info('Path: ' + path2 + ' Result: ' + gjp.read(path2)); +gs.info('Path: ' + path3 + ' Result: ' + gjp.read(path3)); +gs.info('Path: ' + path4 + ' Result: ' + gjp.read(path4)); +gs.info('Path: ' + path5 + ' Result: ' + gjp.read(path5)); +gs.info('Path: ' + path6 + ' Result: ' + gjp.read(path6)); \ No newline at end of file