JSONPath for Python

1.1.5 · active · verified Thu Apr 09

jsonpath-python is a lightweight and powerful implementation of the JSONPath specification for Python, enabling users to extract data from JSON objects using XPath-like expressions. It is actively maintained, with version 1.1.5 being the current release, and follows a moderate release cadence addressing fixes, performance, and security.

Warnings

Install

Imports

Quickstart

Initializes a JSONPath object with an expression and uses its `parse` method to extract matching values from a dictionary. The `parse` method returns a list of all matched items. Other methods like `match` (returns first match) and `findall` (returns generator) are also available.

from jsonpath 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}
    }
}

# Find all authors of books
path_expression = '$..book[*].author'
path = JSONPath(path_expression)
authors = path.parse(data)
print(f"Authors: {authors}")

# Find all prices
all_prices = JSONPath('$..price').parse(data)
print(f"All prices: {all_prices}")

view raw JSON →