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
About the Author
About the Reviewers

Using JSONPath in your web application

Using JSONPathwith JavaScript in your web application is easy. You only need to include the jsonpath.js implementation in your application, and then use its jsonPath function.

Getting ready

Before you begin, you need to download the JavaScript jsonpath library from and include it in the scripts your HTML page uses with a script tag, like this:

<script type="text/javascript" src="jsonpath.js"></script>

The jsonPath function takes a JSON object (not as a string, but as a JavaScript object) and applies the path operation to the contents, returning either the matched values or a normalized path. Let's see an example.

How to do it…

Here's an example that returns a list of titles from the JSON object I showed in the introduction:

var o = { /* object from the introduction */ };
var result = jsonPath(o, "$..title");

Note that if you have the object as...