JSONPath (RFC 9535)
jsonpath-rfc9535 is a Python library that provides a comprehensive and compliant implementation of JSONPath, as defined by RFC 9535. It allows users to query and manipulate JSON data structures using standard JSONPath expressions. The library is currently at version 1.0.0 and maintains an active development cadence with regular updates.
Common errors
-
ModuleNotFoundError: No module named 'jsonpath_rfc9535'
cause The Python package name on PyPI is `jsonpath-rfc9535`, but the primary module imported by the library is `jsonpath`.fixChange your import statement from `from jsonpath_rfc9535 import JSONPath` to `from jsonpath import JSONPath`. -
jsonpath.exceptions.JSONPathSyntaxError: Unbalanced parentheses (or similar syntax errors in queries)
cause The JSONPath query string provided contains a syntax error, such as mismatched brackets, invalid escape sequences, or incorrect filter expression formatting.fixCarefully review your JSONPath query against the RFC 9535 specification. Ensure all parentheses, brackets, and string literals are correctly balanced and escaped. Also, ensure you are on the latest library version as previous versions had parsing bugs. -
AttributeError: can't set attribute 'value' (for versions <1.0.0) OR unexpected modification of source data (for versions 1.0.0+)
cause In versions prior to 1.0.0, `JSONPathNode.value` was a read-only attribute, thus attempts to set it would fail. In version 1.0.0+, assigning to `JSONPathNode.value` now modifies the underlying source JSON data, which might be unexpected if you expected immutable behavior.fixFor versions <1.0.0, `JSONPathNode.value` cannot be directly assigned. For v1.0.0+, be aware that `node.value = new_value` updates the original data. If you need a local, immutable copy, first store `node.value` in a new variable before modification.
Warnings
- breaking In version 1.0.0, the `JSONPathNode.value` attribute was changed to a property with a setter. Assigning a new value to `node.value` will now modify the original source JSON data structure. In previous versions, `value` was read-only.
- gotcha Early versions (pre-1.0.0) had several bug fixes related to parsing complex filter expressions, escape sequences, number literals, and bracketed segments. Using older versions with complex or malformed JSONPath queries could lead to `JSONPathSyntaxError` or incorrect results.
- gotcha The `search()` and `match()` functions validate I-Regexp patterns according to RFC 9485. If an invalid pattern is provided, these functions will return `_LogicalFalse` instead of raising an exception. This behavior was introduced in v0.1.2.
Install
-
pip install jsonpath-rfc9535
Imports
- JSONPath
from jsonpath_rfc9535 import JSONPath
from jsonpath import JSONPath
Quickstart
from jsonpath import JSONPath
data = {
"store": {
"book": [
{ "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.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 }
}
}
# Find all book titles
path = JSONPath("$.store.book[*].title")
for node in path.findall(data):
print(node.value)
# Find books cheaper than 10
path_cheap_books = JSONPath("$.store.book[?(@.price < 10)]")
for node in path_cheap_books.findall(data):
print(node.value)