JSONPath Next-Generation for Python

1.6.1 · active · verified Fri Apr 10

bc-jsonpath-ng is a robust and extended implementation of JSONPath for Python, aiming for standard compliance. It includes arithmetic and binary comparison operators and provides a clear Abstract Syntax Tree (AST) for metaprogramming, merging functionalities from `jsonpath-rw` and `jsonpath-rw-ext`. The current version is 1.6.1, and it receives active maintenance and updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse JSON data and extract values using JSONPath expressions. It shows basic selection, filtering, and how to retrieve both the value and the full path of matches.

import json
from jsonpath_ng import jsonpath, parse

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

# Example 1: Find all book titles
jsonpath_expr_titles = parse('$.store.book[*].title')
titles = [match.value for match in jsonpath_expr_titles.find(data)]
print(f"Book Titles: {titles}")

# Example 2: Find prices of books cheaper than 10
jsonpath_expr_cheap_books = parse('$.store.book[?price < 10].price')
cheap_prices = [match.value for match in jsonpath_expr_cheap_books.find(data)]
print(f"Prices of cheap books: {cheap_prices}")

# Example 3: Accessing full path of matches
jsonpath_expr_all_prices = parse('$.store..price')
for match in jsonpath_expr_all_prices.find(data):
    print(f"Full path: {match.full_path}, Value: {match.value}")

view raw JSON →