ObjectPath (Python)

0.6.1 · maintenance · verified Thu Apr 16

ObjectPath is a Python library that provides an agile query language for semi-structured data, particularly JSON. It allows for powerful querying similar to XPath or JSONPath, incorporating arithmetic calculations, comparisons, and built-in functions. The current version is 0.6.1, released in November 2018. The original Python implementation (adriank/ObjectPath) is currently not actively maintained, which is an important consideration for new projects.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create an ObjectPath Tree from a JSON-like Python dictionary and execute basic queries to extract data. It shows how to retrieve all authors, filter books by price, and access specific nested properties.

import json
from objectpath import Tree

data = {
    "store": {
        "book": [
            {"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95},
            {"category": "fiction", "author": "E.B. White", "title": "Charlotte's Web", "price": 7.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}
    }
}

tree = Tree(data)

# Find all authors
authors = tree.execute("$.store.book[*].author")
print(f"Authors: {list(authors)}")

# Find books cheaper than 10
cheap_books = tree.execute("$.store.book[(@.price < 10)].title")
print(f"Cheap books: {list(cheap_books)}")

# Find the red bicycle's price
bicycle_price = tree.execute("$.store.bicycle.price")
print(f"Bicycle price: {bicycle_price}")

view raw JSON →