Using JSONPath scripting to construct more complicated queries
Sometimes, what you really want to do is query all items that meet a certain criteria, such as those exceed a particular threshold. JSONPath provides the ?()
predicate, which lets you execute simple comparison scripts of individual fields in your JSONPath.
How to do it…
Here's an example that queries all books costing less than 10
currency units:
$.store.book[?(@.price < 10)].title
How it works…
The query begins by specifying all book items in the store; the ?()
predicate then selects each item in that category using the @
selector to obtain the value of the current item, and then selects prices less than 10
. The resulting items have their title field extracted. This query yields the following results:
[ "Sayings of the Century", "Moby Dick" ]
Queries like this don't work with all implementations of JSONPath. Checking the JSONPath Expression tester at http://jsonpath.curiousconcept.com/, I found that it worked using the...