API Docs¶
- jsonpath_rfc9535.compile(expr: str) JSONPathQuery¶
Compile JSONPath query expr using the default configuration.
- Parameters:
expr – A JSONPath query expression.
- Returns:
query – A compiled query ready to match against JSON-like data.
- Raises:
JSONPathNameError – If a function extension can not be resolved.
JSONPathRecursionError – If expr is crafted in such a way to hit Python’s recursion limit.
JSONPathSyntaxError – If expr is syntactically invalid.
JSONPathTypeError – If expr is semantically invalid - Calling a function extension with arguments of the wrong type or arity, for example.
- jsonpath_rfc9535.find(expr: str, data: object) JSONPathNodeList¶
Return nodes found by apply query expression expr to data using the default configuration.
- Parameters:
expr – A JSONPath query expression.
data – JSON-like data to apply expr to.
- Returns:
JSONPathNodeList – A list of JSON-like values and their location in data.
- Raises:
JSONPathNameError – If a function extension can not be resolved.
JSONPathRecursionError – If expr is crafted in such a way to hit Python’s recursion limit. Or if a descendent segment visits nodes to a depth that exceeds the configured max_recursion_depth.
JSONPathSyntaxError – If expr is syntactically invalid.
JSONPathTypeError – If expr is semantically invalid - Calling a function extension with arguments of the wrong type or arity, for example.
- jsonpath_rfc9535.findall(expr: str, data: object) list[object]¶
Return all values found from applying query expression expr to data.
Use findall if you just need values from data, without their location or normalized path. It can be significantly faster and more memory efficient than find` or finditer.
- Parameters:
expr – A JSONPath query expression.
data – JSON-like data to apply expr to.
- Returns:
list[object] – All values found by applying expr to data.
- Raises:
JSONPathError – The same as find.
See also
- jsonpath_rfc9535.finditer(expr: str, data: object) Iterable[JSONPathNode]¶
Generate nodes from applying query expression expr to data using the default configuration.
- Parameters:
expr – A JSONPath query expression.
data – JSON-like data to apply expr to.
- Yields:
JSONPathNode – Matched JSON-like values and their locations in data.
- Raises:
JSONPathError – The same as find.
See also
- jsonpath_rfc9535.search(expr: str, data: object) JSONPathNode | None¶
Return the first node found by applying query expression expr to data.
- Parameters:
expr – A JSONPath query expression.
data – JSON-like data to apply expr to.
- Returns:
node (JSONPathNode, Optional) – The first available JSONPathNode instance, or None if there are no matches.
- Raises:
JSONPathError – The same as find.
See also
- class jsonpath_rfc9535.JSONPathEnvironment(*, max_index: int = 9007199254740991, min_index: int = -9007199254740991, max_recursion_depth: int = 100, max_expression_depth: int = 30, parser: Parser = <class 'jsonpath_rfc9535._parser_standard.StandardParser'>, composite_types: Literal['strict', 'native', 'abstract']='native')¶
JSONPath parser and evaluator configuration.
- Parameters:
max_index – The maximum integer allowed when selecting array items by index.
min_index – The minimum integer allowed when selecting array items by index.
max_expression_depth – The maximum number of nested expressions allowed in a query before a JSONPathRecursionError is raised.
max_recursion_depth – The maximum depth the descendant segment can go before a JSONPathRecursionError is raised.
parser – The JSONPath query parser to use. You can subclass
StandardParserto customize JSONPath parsing behavior.composite_types –
One of
"strict","native"or"abstract".In order from fastest to slowest, the strict strategy works with data composed of
listanddictinstances. The native strategy works with data composed of lists, dicts and list/dict subclasses. And the abstract strategy works with data composed of Mapping and Sequence types.
- functions¶
A map of JSONPath function extension names to implementation - classes inheriting from FunctionExtension.
- compile(expr: str) JSONPathQuery¶
Prepare JSONPath query expr for repeated application to different data.
- Parameters:
expr – A JSONPath query expression.
- Returns:
query – A compiled query ready to match against JSON-like data.
- Raises:
JSONPathNameError – If a function extension can not be resolved.
JSONPathRecursionError – If expr is crafted in such a way to hit Python’s recursion limit.
JSONPathSyntaxError – If expr is syntactically invalid.
JSONPathTypeError – If expr is semantically invalid - Calling a function extension with arguments of the wrong type or arity, for example.
- find(expr: str, data: object) JSONPathNodeList¶
Apply JSONPath query expr to data and return a list of nodes.
- Parameters:
expr – A JSONPath query expression.
data – JSON-like data to apply expr to.
- Returns:
JSONPathNodeList – A list of JSON-like values and their location in data.
- Raises:
JSONPathNameError – If a function extension can not be resolved.
JSONPathRecursionError – If expr is crafted in such a way to hit Python’s recursion limit. Or if a descendent segment visits nodes to a depth that exceeds the configured max_recursion_depth.
JSONPathSyntaxError – If expr is syntactically invalid.
JSONPathTypeError – If expr is semantically invalid - Calling a function extension with arguments of the wrong type or arity, for example.
- findall(expr: str, data: object) list[object]¶
Apply JSONPath query expr to data and return a list of values.
- Parameters:
expr – A JSONPath query expression.
data – JSON-like data to apply expr to.
- Returns:
list[object] – All values found by applying expr to data.
- Raises:
JSONPathError – The same as find.
See also
- finditer(expr: str, data: object) Iterable[JSONPathNode]¶
Generate nodes from applying query expression expr to data.
- Parameters:
expr – A JSONPath query expression.
data – JSON-like data to apply expr to.
- Yields:
JSONPathNode – The matched value and its location in data.
- Raises:
JSONPathError – The same as find.
See also
- parse(expr: str) JSONPathQuery¶
An alias for compile.
See also
- search(expr: str, data: object) JSONPathNode | None¶
Return the first node found by applying query expression expr to data.
- Parameters:
expr – A JSONPath query expression.
data – JSON-like data to apply expr to.
- Returns:
node (JSONPathNode, Optional) – The first available JSONPathNode instance, or None if there are no matches.
- Raises:
JSONPathError – The same as find.
See also
- class jsonpath_rfc9535.JSONPathQuery(env: JSONPathEnvironment, segments: tuple[Segment, ...])¶
A compiled JSONPath expression ready to be applied to JSON-like data.
- Parameters:
env – The JSONPath configuration object this query is bound to.
segments – Our internal representation of a compiled JSONPath query.
- find(data: object) JSONPathNodeList¶
Apply this query to data and return a list of nodes.
- Parameters:
data – JSON-like data to apply this query to.
- Returns:
JSONPathNodeList – The list of nodes found by applying this query to data
- Raises:
JSONPathRecursionError – If a descendent segment visits nodes to a depth that exceeds the configured max_recursion_depth.
- find_one(data: object) JSONPathNode | None¶
An alias for search.
See also
- findall(data: object) list[object]¶
Apply this query to data and return a list of values.
- Parameters:
data – JSON-like data to apply this query to.
- Returns:
list[object] – All values found by applying this query to data.
- Raises:
JSONPathRecursionError – If a descendent segment visits nodes to a depth that exceeds the configured max_recursion_depth.
- finditer(data: object) Iterable[JSONPathNode]¶
Generate nodes by applying this query to data.
- Parameters:
data – JSON-like data to apply this query to.
- Yields:
JSONPathNode – The matched value and its location in data.
- Raises:
JSONPathRecursionError – If a descendent segment visits nodes to a depth that exceeds the configured max_recursion_depth.
- search(data: object) JSONPathNode | None¶
Return the first node found by applying this query to data.
- Parameters:
data – JSON-like data to apply this query to.
- Returns:
node (JSONPathNode, Optional) – The first available JSONPathNode instance, or None if there are no matches.
- Raises:
JSONPathRecursionError – If a descendent segment visits nodes to a depth that exceeds the configured max_recursion_depth.
- class jsonpath_rfc9535.NodeList(iterable=(), /)¶
A list of tuple nodes.
- class jsonpath_rfc9535.JSONPathNode(node: Node)¶
A JSON-like value and its location in a JSON document.
- Parameters:
node – A tuple node generated by the JSONPath evaluator.
- value¶
The JSON-like value associated with this node.
Assigning to value will update this node and mutate source data too. Mutating source data after evaluating a query can invalidate child nodes. Use at your own risk.
- Type:
- location¶
Property names and array indexes that make up the normalized path to value.
- parent() JSONPathNode | None¶
Return this node’s direct parent, or None if this is the document root.
- class jsonpath_rfc9535.JSONPathNodeList(iterable=(), /)¶
A list of JSONPath nodes with some helper methods.
- jsonpath_rfc9535.check_iregexp(pattern: str) bool¶
Return True if pattern conforms to RFC 9485, or False otherwise.
- jsonpath_rfc9535.map_iregexp(pattern: str, *, escape_anchors: bool = True, wrap: bool = True) str¶
Map I-Regexp pattern to a “regex” pattern.
Assumes pattern has already passed RFC 9485 validation.
- Parameters:
pattern – A regular expression pattern following RFC 9485 syntax.
escape_anchors – When True, treat
^and$as normal characters.wrap – When True, wrap the result in
^(?:and)$.
- Returns:
pattern – A regular expression pattern suitable for the “regex” package. The result is not guaranteed to be compatible with Python’s “re” module due to the lack of support for
\p{..}and\P{..}escape sequences.
Query resolvers¶
- class jsonpath_rfc9535.BasicResolverStrict(env: JSONPathEnvironment, root: object)¶
A JSONPath resolver using strict JSON type checks.
- class jsonpath_rfc9535.BasicResolverNative(env: JSONPathEnvironment, root: object)¶
A JSONPath resolver using native JSON type checks.
- class jsonpath_rfc9535.BasicResolverAbstract(env: JSONPathEnvironment, root: object)¶
A JSONPath resolver using abstract JSON type checks.
- class jsonpath_rfc9535.StandardResolverStrict(env: JSONPathEnvironment, root: object)¶
A JSONPath resolver using strict JSON type checks.
- class jsonpath_rfc9535.StandardResolverNative(env: JSONPathEnvironment, root: object)¶
A JSONPath resolver using native JSON type checks.
- class jsonpath_rfc9535.StandardResolverAbstract(env: JSONPathEnvironment, root: object)¶
A JSONPath resolver using abstract JSON type checks.
Function Extensions¶
- type jsonpath_rfc9535.functions.ExpressionType = Literal[1, 2, 3]¶
An integer indicating a function extension argument type or return type.
- class jsonpath_rfc9535.functions.FunctionExtension¶
Bases:
ABCThe base class for all JSONPath function extensions.
- abstract property arg_types: Sequence[ExpressionType]¶
Argument types expected by this function extension.
- abstract property return_type: ExpressionType¶
The type of this function extension’s return value.
- class jsonpath_rfc9535.functions.Count¶
Bases:
FunctionExtension
- class jsonpath_rfc9535.functions.Length¶
Bases:
FunctionExtension
- class jsonpath_rfc9535.functions.CachingRegexFunction(*, cache_capacity: int = 300, debug: bool = False, escape_anchors: bool = False, thread_safe: bool = False)¶
Bases:
FunctionExtensionThe base class for caching regex functions using the regex package.
Function extensions inheriting from this class perform I-Regexp validation on all regex patterns and map I-Regexp patterns to regex compatible patterns.
- Parameters:
cache_capacity – The maximum size of the regex cache before the least recently used patter is discarded.
debug – When True, raise JSONPathError if a pattern is invalid. Otherwise invalid patterns are silently ignored.
escape_anchors – When True, treat
^and$as normal characters.thread_safe – When True, use the thread safe LRU cache.
- class jsonpath_rfc9535.functions.Match(*, cache_capacity: int = 300, debug: bool = False, escape_anchors: bool = False, thread_safe: bool = False)¶
Bases:
CachingRegexFunctionThe standard match function with I-Regex validation.
- class jsonpath_rfc9535.functions.Search(*, cache_capacity: int = 300, debug: bool = False, escape_anchors: bool = False, thread_safe: bool = False)¶
Bases:
CachingRegexFunctionThe standard search function with I-Regex validation.
- class jsonpath_rfc9535.functions.Value¶
Bases:
FunctionExtension
- class jsonpath_rfc9535.functions.CachingReFunction(*, cache_capacity: int = 300, debug: bool = False, thread_safe: bool = False)¶
Bases:
FunctionExtensionThe base class for caching regex functions using Python’s re module.
Function extensions inheriting from this class do not validate re patterns against I-Regexp.
- Parameters:
cache_capacity – The maximum size of the regex cache before the least recently used patter is discarded.
debug – When True, raise JSONPathError if a pattern is invalid. Otherwise invalid patterns are silently ignored.
thread_safe – When True, use the thread safe LRU cache.
- class jsonpath_rfc9535.functions.PyMatch(*, cache_capacity: int = 300, debug: bool = False, thread_safe: bool = False)¶
Bases:
CachingReFunctionA non-standard match function using Python regular expressions.
- class jsonpath_rfc9535.functions.PySearch(*, cache_capacity: int = 300, debug: bool = False, thread_safe: bool = False)¶
Bases:
CachingReFunctionA non-standard search function using Python regular expressions.
Exceptions¶
- class jsonpath_rfc9535.JSONPathError¶
The base class for all JSONPath exceptions.
- class jsonpath_rfc9535.DetailedJSONPathError(msg: str, token: Token, source: str)¶
A JSONPath exception with contextual information.
- class jsonpath_rfc9535.JSONPathNameError(msg: str, token: Token, source: str)¶
The exception raised when a function extension can not be resolved.
- class jsonpath_rfc9535.JSONPathSyntaxError(msg: str, token: Token, source: str)¶
The exception raised due to a malformed JSONPath query.
- class jsonpath_rfc9535.JSONPathIndexError(msg: str, token: Token, source: str)¶
The exception raised when an index selector is out of range.
- class jsonpath_rfc9535.JSONPathTypeError(msg: str, token: Token, source: str)¶
The exceptions raised when a JSONPath query is not well typed.
- class jsonpath_rfc9535.JSONPathRecursionError¶
The exception raised when a recursion limit is reached.
This could be raised at parse time if a JSONPath query contains deeply nested expressions. Or at query resolution time if max_recursion_depth is reached by the descendant segment (..).