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 Node.js application


There's an npm package available that contains an implementation of the JavaScript JSONPath implementation, so if you want to use JSONPath from Node.js, you only need to install the JSONPath module and call it directly.

Getting ready

To install the JSONPath module, run the following command to include the module in your current application:

npm install JSONPath

Alternatively, you can run the following command to include it for all projects on your system:

npm install –g JSONPath

Next, you'll have to require the module in your source code, like this:

var jsonPath = require('JSONPath');

This loads the JSONPath module into your environment, storing a reference in the jsonPath variable.

How to do it…

The JSONPath module for Node.js defines a single method, eval, which takes a JavaScript object and a path to evaluate. For example, to obtain a list of the titles in our example document, we would need to execute the following code:

var jsonPath = require('JSONPath...