You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
NOTE: If you're coming from [Python JSONPath](https://github.com/jg-rp/python-jsonpath), this API is similar but different. Neither implementation is guaranteed to be a drop-in replacement for the other.
30
+
31
+
### `query(path, data)`
32
+
33
+
Apply JSONPath expression _path_ to _data_. _data_ should arbitrary, possible nested, Python dictionaries, lists, strings, integers, floats, booleans or `None`, as you would get from [`json.load()`](https://docs.python.org/3/library/json.html#json.load).
34
+
35
+
A list of `JSONPathNode` instances is returned, one for each value in _data_ matched by _path_. The returned list will be empty if there were no matches.
36
+
37
+
Each `JSONPathNode` has:
38
+
39
+
- a `value` property, which is the JSON-like value associated with the node.
40
+
- a `parts` property, which is a tuple of property names and array/list indices that were required to reach the node's value in the target JSON document.
41
+
- a `path()` method, which returns the normalized path to the node in the target JSON document.
42
+
43
+
The returned list is a subclass of `list` with some helper methods.
44
+
45
+
-`values()` returns a list of values, one for each node.
46
+
-`values_or_singular()` returns a scalar value if the list has exactly one node, or a list of values otherwise.
47
+
48
+
**Example:**
49
+
50
+
```python
51
+
from jsonpath_rfc9535 import query
52
+
53
+
data = {
54
+
"users": [
55
+
{"name": "Sue", "score": 100},
56
+
{"name": "John", "score": 86, "admin": True},
57
+
{"name": "Sally", "score": 84, "admin": False},
58
+
{"name": "Jane", "score": 55},
59
+
],
60
+
"moderator": "John",
61
+
}
62
+
63
+
for node in query("$.users[?@.score > 85]", data):
64
+
print(f"{node.value} at '{node.path()}'")
65
+
66
+
# {'name': 'Sue', 'score': 100} at '$['users'][0]'
67
+
# {'name': 'John', 'score': 86, 'admin': True} at '$['users'][1]'
68
+
```
69
+
70
+
### findall(path, data)
71
+
72
+
`findall()` accepts the same arguments as [`query()`](#querypath-data), but returns a list of value rather than a list of nodes.
73
+
74
+
`findall()` is equivalent to `query("$.some.thing", data).values()`.
75
+
76
+
### finditer(path, data)
77
+
78
+
`finditer()` accepts the same arguments as [`query()`](#querypath-data), but returns an iterator over `JSONPathNode` instances rather than a list. This could be useful if you're expecting a large number of results that you don't want to load into memory all at once.
0 commit comments