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

Get JavaScript JSON Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.