JSONPath, JSON Pointer and JSON Patch for Python

2.0.2 · active · verified Sat Apr 11

Python JSONPath is a flexible engine for processing JSONPath, JSON Pointer, and JSON Patch expressions. It aims to be compliant with RFC 9535 for JSONPath, RFC 6901 for JSON Pointer, and RFC 6902 for JSON Patch. The library is currently at version 2.0.2 and receives regular updates and maintenance, often with a few releases per year.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic JSONPath queries using `findall()` and `compile()` to extract data from a sample JSON structure. It shows how to select elements, filter by conditions, and compile paths for efficiency.

import jsonpath

data = {
    "store": {
        "book": [
            {"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95},
            {"category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99},
            {"category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99},
            {"category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99}
        ],
        "bicycle": {"color": "red", "price": 19.95}
    },
    "expensive": 10
}

# Find the titles of all books
book_titles = jsonpath.findall("$.store.book[*].title", data)
print(f"Book Titles: {book_titles}")

# Find all books cheaper than $10
cheap_books = jsonpath.findall("$.store.book[?(@.price < 10)].title", data)
print(f"Cheap Book Titles: {cheap_books}")

# Compile a JSONPath expression for repeated use
compiled_path = jsonpath.compile("$.store.book[?(@.category == 'fiction')].author")
fiction_authors = compiled_path.findall(data)
print(f"Fiction Authors: {fiction_authors}")

view raw JSON →