@@ -4,6 +4,12 @@ Python JSONPath Next-Generation
4
4
A final implementation of JSONPath for Python, including arithmetic
5
5
and binary comparison operators, as defined in the original `JSONPath proposal `_.
6
6
7
+ This packages merges both `jsonpath-rw `_ and `jsonpath-rw-ext `_ and
8
+ provides several AST API enhancements, such as the ability to update or removes nodes in the tree.
9
+
10
+ About
11
+ -----
12
+
7
13
This library provides a robust and significantly extended implementation
8
14
of JSONPath for Python. It is tested with Python 2.6, 2.7, 3.2, 3.3.
9
15
@@ -19,7 +25,7 @@ To install, use pip:
19
25
20
26
::
21
27
22
- $ pip install git+git://github.com/tomas-fp/python-jsonpath-ng.git#egg= jsonpath-ng
28
+ $ pip install --upgrade jsonpath-ng
23
29
24
30
Then:
25
31
56
62
>> > jsonpath_expr_direct = Fields(' foo' ).child(Slice(' *' )).child(Fields(' baz' )) # This is equivalent
57
63
58
64
65
+ Using the extended parser:
66
+
67
+ .. code :: python
68
+
69
+ $ python
70
+
71
+ >> > from jsonpath_ng.ext import parse
72
+
73
+ # A robust parser, not just a regex. (Makes powerful extensions possible; see below)
74
+ >> > jsonpath_expr = parse(' foo[*].baz' )
75
+
76
+
59
77
JSONPath Syntax
60
78
---------------
61
79
@@ -144,8 +162,9 @@ easy to do so directly, and here are some examples:
144
162
- ``Where(Slice(), Fields('subfield')) ``
145
163
- ``Descendants(jsonpath, jsonpath) ``
146
164
147
- Extensions
148
- ----------
165
+
166
+ Extras
167
+ ------
149
168
150
169
- *Path data *: The result of ``JsonPath.find `` provide detailed context
151
170
and path data so it is easy to traverse to parent objects, print full
@@ -160,6 +179,69 @@ Extensions
160
179
contained in backquotes can be made to be a new operator, currently
161
180
by extending the library.
162
181
182
+
183
+ Extensions
184
+ ----------
185
+
186
+ +--------------+----------------------------------------------+
187
+ | name | Example |
188
+ +==============+==============================================+
189
+ | len | - $.objects.`len` |
190
+ +--------------+----------------------------------------------+
191
+ | sub | - $.field.`sub(/foo\\\\ +(.*)/, \\\\ 1)` |
192
+ +--------------+----------------------------------------------+
193
+ | split | - $.field.`split(+, 2, -1)` |
194
+ | | - $.field.`split(sep, segement, maxsplit)` |
195
+ +--------------+----------------------------------------------+
196
+ | sorted | - $.objects.`sorted` |
197
+ | | - $.objects[\\ some_field] |
198
+ | | - $.objects[\\ some_field,/other_field] |
199
+ +--------------+----------------------------------------------+
200
+ | filter | - $.objects[?(@some_field > 5)] |
201
+ | | - $.objects[?some_field = "foobar")] |
202
+ | | - $.objects[?some_field > 5 & other < 2)] |
203
+ +--------------+----------------------------------------------+
204
+ | arithmetic | - $.foo + "_" + $.bar |
205
+ | (-+*/) | - $.foo * 12 |
206
+ | | - $.objects[*].cow + $.objects[*].cat |
207
+ +--------------+----------------------------------------------+
208
+
209
+ About arithmetic and string
210
+ ---------------------------
211
+
212
+ Operations are done with python operators and allows types that python
213
+ allows, and return [] if the operation can be done due to incompatible types.
214
+
215
+ When operators are used, a jsonpath must be be fully defined otherwise
216
+ jsonpath-rw-ext can't known if the expression is a string or a jsonpath field,
217
+ in this case it will choice string as type.
218
+
219
+ Example with data::
220
+
221
+ {
222
+ 'cow': 'foo',
223
+ 'fish': 'bar'
224
+ }
225
+
226
+ | **cow + fish** returns **cowfish**
227
+ | **$.cow + $.fish** returns **foobar**
228
+ | **$.cow + "_" + $.fish** returns **foo_bar**
229
+ | **$.cow + "_" + fish** returns **foo_fish**
230
+
231
+ About arithmetic and list
232
+ -------------------------
233
+
234
+ Arithmetic can be used against two lists if they have the same size.
235
+
236
+ Example with data::
237
+
238
+ {'objects': [
239
+ {'cow': 2, 'cat': 3},
240
+ {'cow': 4, 'cat': 6}
241
+ ]}
242
+
243
+ | **$.objects[\*].cow + $.objects[\*].cat** returns **[6, 9]**
244
+
163
245
More to explore
164
246
---------------
165
247
@@ -211,6 +293,7 @@ Copyright and License
211
293
---------------------
212
294
213
295
Copyright 2013 - Kenneth Knowles
296
+ Copyright 2017 - Tomas Aparicio
214
297
215
298
Licensed under the Apache License, Version 2.0 (the "License"); you may
216
299
not use this file except in compliance with the License. You may obtain
@@ -227,6 +310,9 @@ See the License for the specific language governing permissions and
227
310
limitations under the License.
228
311
229
312
.. _`JSONPath proposal` : http://goessner.net/articles/JsonPath/
313
+ .. _`jsonpath-rw` : https://github.com/kennknowles/python-jsonpath-rw
314
+ .. _`jsonpath-rw-ext` : https://pypi.python.org/pypi/jsonpath-rw-ext/
315
+
230
316
.. |Build Status | image :: https://travis-ci.org/kennknowles/python-jsonpath-ng.png?branch=master
231
317
:target: https://travis-ci.org/kennknowles/python-jsonpath-ng
232
318
.. |Test coverage | image :: https://coveralls.io/repos/kennknowles/python-jsonpath-ng/badge.png?branch=master
0 commit comments