Book Image

JavaScript JSON Cookbook

By : Ray Rischpater, Brian Ritchie, Ray Rischpater
Book Image

JavaScript JSON Cookbook

By: Ray Rischpater, Brian Ritchie, Ray Rischpater

Overview of this book

Table of Contents (17 chapters)
JavaScript JSON Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 of all price fields for every item in the store, and the fourth example returns the fourth book in the store.