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
Copy file name to clipboardExpand all lines: README.md
+9-17Lines changed: 9 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,18 +26,16 @@ TODO:
26
26
27
27
## Usage
28
28
29
-
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.
29
+
### `find(query, value)`
30
30
31
-
### `query(path, data)`
31
+
Apply JSONPath expression _query_ to _value_. _value_ 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).
32
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 node for each value in _data_ matched by _path_. The returned list will be empty if there were no matches.
33
+
A list of `JSONPathNode` instances is returned, one node for each value matched by _path_. The returned list will be empty if there were no matches.
36
34
37
35
Each `JSONPathNode` has:
38
36
39
37
- 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.
38
+
- a `location` property, which is a tuple of property names and array/list indexes that were required to reach the node's value in the target JSON document.
41
39
- a `path()` method, which returns the normalized path to the node in the target JSON document.
42
40
43
41
The returned list is a subclass of `list` with some helper methods.
@@ -48,9 +46,9 @@ The returned list is a subclass of `list` with some helper methods.
48
46
**Example:**
49
47
50
48
```python
51
-
from jsonpath_rfc9535 importquery
49
+
from jsonpath_rfc9535 importfind
52
50
53
-
data= {
51
+
value= {
54
52
"users": [
55
53
{"name": "Sue", "score": 100},
56
54
{"name": "John", "score": 86, "admin": True},
@@ -60,22 +58,16 @@ data = {
60
58
"moderator": "John",
61
59
}
62
60
63
-
for node inquery("$.users[?@.score > 85]", data):
61
+
for node infind("$.users[?@.score > 85]", value):
64
62
print(f"{node.value} at '{node.path()}'")
65
63
66
64
# {'name': 'Sue', 'score': 100} at '$['users'][0]'
67
65
# {'name': 'John', 'score': 86, 'admin': True} at '$['users'][1]'
68
66
```
69
67
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)
68
+
### finditer(query, value)
77
69
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.
70
+
`finditer()` accepts the same arguments as [`find()`](#findquery-value), 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