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 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 https://code.google.com/p/jsonpath/ and include it in the scripts your HTML page uses with a script tag, like this:

<html>
<head>
<title>…</title>
<script type="text/javascript" src="jsonpath.js"></script>
</head>

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