JSONPath for Python

0.82.2 · active · verified Sat Apr 11

The `jsonpath` library provides a Pythonic implementation of JSONPath, following the IETF JSONPath draft specification. It allows users to query JSON-like data structures using XPath-like expressions. It's actively developed, with the latest version being 0.82.2, and typically releases updates as the IETF draft progresses or new features are added.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `jsonpath.select` convenience function for simple queries, and how to use the `jsonpath.JSONPath` class for compiling and reusing path expressions, which is more efficient for repeated queries against different data.

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}
        ],
        "bicycle": {"color": "red", "price": 19.95}
    }
}

# Use the convenience 'select' function to query data
path_expression = "$.store.book[*].author"
authors = jsonpath.select(path_expression, data)
print(f"Authors found: {authors}")

# For performance-critical scenarios, compile the path once using JSONPath class
from jsonpath import JSONPath
json_path = JSONPath(path_expression)
authors_compiled = json_path.findall(data)
print(f"Authors (compiled path): {authors_compiled}")

view raw JSON →