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 the JSONPath dot-notation to query JSON documents


JSONPath uses expressions written in either the dot-notation or bracket-notation to denote a traversal of fields in the JSON document. Dots separate field names, as if they were object attributes.

How to do it…

Here are a few examples of dot-notation:

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

How it works…

In the first line, we reference the first (counting from zero) book in the store, returning the title field. The second line is similar, except that it returns a collection of all titles of all the books. The third example returns a collection of all price fields in all records in the store collection. The fourth example finds the fourth book item in the store.

The notation is fairly intuitive, except for the use of .. and *. These are examples of some of the special characters used by JSONPath to denote slices across the document.

There's more…

JSONPath defines the following special characters you can use when...