Python JSONPath2 Module

The jsonpath2 module.

jsonpath2.match(path_str: str, root_value: object) → Generator[jsonpath2.node.MatchData, None, None][source]

Match root value of the path.

The jsonpath2.match function is a shortcut to match a given JSON data structure against a JSONPath string.

>>> import jsonpath2
>>> doc = {'hello': 'Hello, world!'}
>>> [x.current_value for x in jsonpath2.match('$.hello', doc)]
['Hello, world!']

Expression module.

class jsonpath2.expression.Expression[source]

Add the expression methods to the jsonpath object.

_abc_impl = <_abc_data object>
evaluate(root_value: object, current_value: object) → bool[source]

Abstract method to evaluate the expression.

The parse tree node module.

class jsonpath2.node.MatchData(node, root_value, current_value)[source]

Match data object for storing node values.

The jsonpath2.node.MatchData class represents the JSON value and context for a JSONPath match.

This class is constructed with respect to a root JSON value, a current JSON value, and an abstract syntax tree node.

Attributes:
  • root_value The root JSON value.
  • current_value The current JSON value (i.e., the matching JSON value).
  • node The abstract syntax tree node.
__init__(node, root_value, current_value)[source]

Constructor to save root and current node values.

class jsonpath2.node.Node[source]

Node object for the jsonpath parsetree.

The jsonpath2.node.Node class represents the abstract syntax tree for a JSONPath.

_abc_impl = <_abc_data object>
match(root_value: object, current_value: object) → Generator[jsonpath2.node.MatchData, None, None][source]

Abstract method to determine a node match.

Match the given root and current JSON data structures against this instance. For each match, yield an instance of the jsonpath2.node.MatchData class.

The path module.

class jsonpath2.path.Path(root_node: jsonpath2.nodes.root.RootNode)[source]

Path parsetree object.

The jsonpath2.path.Path class represents a JSONPath.

>>> s = '{"hello":"Hello, world!"}'
'{"hello":"Hello, world!"}'
>>> import json
>>> d = json.loads(s)
{'hello':'Hello, world!'}
>>> from jsonpath2.path import Path
>>> p = Path.parse_str('$["hello"]')
<jsonpath2.path.Path object>
>>> [match_data.current_value for match_data in p.match(d)]
['Hello, world!']
>>> [match_data.node.tojsonpath() for match_data in p.match(d)]
['$["hello"]']

This class is constructed with respect to the given instance of the jsonpath2.nodes.root.RootNode class (viz., the root_node property).

Attributes:
  • root_node The root node of the abstract syntax tree for this instance.
__init__(root_node: jsonpath2.nodes.root.RootNode)[source]

Constructor saving the root node.

match(root_value: object) → Generator[jsonpath2.node.MatchData, None, None][source]

Match root value of the path.

Match the given JSON data structure against this instance. For each match, yield an instance of the jsonpath2.node.MatchData class.

classmethod parse_file(*args, **kwargs)[source]

A handler to parse a file.

Parse the contents of the given file and return a new instance of this class.

classmethod parse_str(*args, **kwargs)[source]

A handler to parse a string.

Parse the given string and return a new instance of this class.

The Subscript module.

class jsonpath2.subscript.Subscript[source]

Subscript has no value beyond a node other than type.

_abc_impl = <_abc_data object>
match(root_value: object, current_value: object) → Generator[jsonpath2.node.MatchData, None, None][source]

Abstract method to determine a node match.

A JSONPath abstract class.

class jsonpath2.tojsonpath.ToJSONPath[source]

Abstract class which calls internal method.

_abc_impl = <_abc_data object>
tojsonpath() → str[source]

Get the json path from self and return it.

Returns the string representation of this instance.