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 Python application


There are several implementations of JSONPath for Python, too. The best is jsonpath-rw library, which provides language extensions so that paths are first-class language objects.

Getting ready

You'll need to install the jsonpath-rw library using pip:

pip install jsonpath-rw

Also, of course, you will need to include the necessary bits of the library when using them:

fromjsonpath_rw import jsonpath, parse

How to do it…

Here's a simple example using our store contents in the introduction stored in the variable object:

>>> object = { … }

>>>path = parse('$..title') 

>>> [match.value for match in path.find(object)]
['Sayings of the Century','Sword of Honour', 'Moby Dick', 'The Lord of the Rings']

How it works…

Processing a path expression using this library is a little like matching a regular expression; you parse out the JSONPath expression and then apply it to the Python object you want to slice using path's find method. This...