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

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