O'Reilly logo

JavaScript JSON Cookbook by Ray Rischpater

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required