Using JSONPath bracket-notation to query JSON documents

JSONPath provides an alternate notation, bracket-notation, which works just like dot-notation to query fields. The syntax is reminiscent of how you access fields in associative arrays, where you pass the field name as the selector to operator[] to obtain the value in the named field.

How to do it…

In bracket notation, we will write the previous recipe's example as follows:

$['store']['book'][0].['title']
$['store']['book'][*].['title']
$['store']..['price']
$..['book'][3]

How it works…

As seen earlier, the first example extracts the title of the first book in the object in the field named store. The second example extracts all titles of all books in the store. The third example returns a collection ...

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.