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
+43-1Lines changed: 43 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,6 +53,8 @@ The JSONPath syntax in this project borrows from [JSONPath - XPath for JSON](htt
53
53
|`?()`| applies a filter expression |
54
54
|`/()`| applies a sorter expression |
55
55
|`()`| applies a field-extractor expression |
56
+
|`=~`| regex match operator (used in filter expressions) |
57
+
|`in`| membership test operator (used in filter expressions) |
56
58
57
59
### Examples
58
60
@@ -144,7 +146,21 @@ Support all python comparison operators (`==`, `!=`, `<`, `>`, `>=`, `<=`), pyth
144
146
[{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95, 'brand': {'version': 'v1.0.0'}}]
145
147
```
146
148
147
-
`Note`: You must use double quote(`""`) instead of single quote(`''`) to wrap the compared string, because single quote(`''`) has another usage in this JSONPath syntax .
149
+
The `in` operator can be used to check membership in lists or substrings in strings:
150
+
151
+
```python
152
+
# Check if a value exists in a list
153
+
>>> data = {"items": [{"tags": ["fruit", "red"]}, {"tags": ["vegetable"]}]}
154
+
>>> JSONPath("$.items[?('fruit' in @.tags)]").parse(data)
155
+
[{'tags': ['fruit', 'red']}]
156
+
157
+
# Check if a substring exists in a string
158
+
>>> data = {"items": [{"name": "apple"}, {"name": "banana"}]}
159
+
>>> JSONPath("$.items[?('app' in @.name)].name").parse(data)
160
+
['apple']
161
+
```
162
+
163
+
`Note`: You must use double quote(`""`) instead of single quote(`''`) to wrap the compared string, because single quote(`''`) has another usage in this JSONPath syntax.
148
164
149
165
#### Sorter Expression
150
166
@@ -188,6 +204,32 @@ Update values in the JSON object using the `update` method.
188
204
# Result: All book prices are multiplied by 0.9
189
205
```
190
206
207
+
## API Reference
208
+
209
+
### Functions
210
+
211
+
-**`search(expr, data)`**: Search JSON data using a JSONPath expression. This function uses LRU caching internally for better performance when the same expression is used multiple times.
212
+
213
+
```python
214
+
>>>from jsonpath import search
215
+
>>> search("$..price", data)
216
+
[8.95, 12.99, 8.99, 22.99, 19.95]
217
+
```
218
+
219
+
-**`compile(expr)`**: Pre-compile a JSONPath expression for reuse. Equivalent to `JSONPath(expr)`.
220
+
221
+
```python
222
+
>>>from jsonpath importcompile
223
+
>>> jp =compile("$.book[*].title")
224
+
>>> jp.parse(data)
225
+
['Sayings of the Century', 'Sword of Honour', 'Moby Dick', 'The Lord of the Rings']
226
+
```
227
+
228
+
### Exceptions
229
+
230
+
-**`ExprSyntaxError`**: Raised when a JSONPath expression has invalid syntax (e.g., using sorter on non-collection types).
231
+
-**`JSONPathTypeError`**: Raised when type-related errors occur (e.g., comparing incompatible types during sorting).
0 commit comments