JSONPath RW Extensions

1.2.2 · active · verified Mon Apr 13

jsonpath-rw-ext extends the capabilities of the jsonpath-rw library by integrating several powerful extensions such as 'len' for list length, 'sorted' for list sorting, 'arithmetic' for mathematical operations, and 'filter' for selective element extraction. These extensions were initially developed to be proposed upstream to jsonpath-rw and persist in jsonpath-rw-ext if not adopted. The library is currently active, with version 1.2.2, though its release cadence appears to be slow or maintenance-focused, with the last update in July 2019.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use the 'len', 'filter', and 'arithmetic' extensions provided by jsonpath-rw-ext to query a sample JSON structure.

import json
import jsonpath_rw_ext

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

# Using the 'len' extension to get the number of books
jsonpath_expr_len = jsonpath_rw_ext.parse('$.store.book.`len`')
matches_len = jsonpath_expr_len.find(data)
print(f"Number of books: {matches_len[0].value}")

# Using the 'filter' extension to find books cheaper than 10
jsonpath_expr_filter = jsonpath_rw_ext.parse('$.store.book[?(@.price < 10)]')
matches_filter = [match.value for match in jsonpath_expr_filter.find(data)]
print(f"Books cheaper than 10: {[book['title'] for book in matches_filter]}")

# Using arithmetic extension
jsonpath_expr_arith = jsonpath_rw_ext.parse('$.store.bicycle.price * 2')
matches_arith = jsonpath_expr_arith.find(data)
print(f"Double bicycle price: {matches_arith[0].value}")

view raw JSON →