@@ -4,6 +4,12 @@ Python JSONPath Next-Generation
44A final implementation of JSONPath for Python, including arithmetic
55and binary comparison operators, as defined in the original `JSONPath proposal `_.
66
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+
713This library provides a robust and significantly extended implementation
814of JSONPath for Python. It is tested with Python 2.6, 2.7, 3.2, 3.3.
915
@@ -19,7 +25,7 @@ To install, use pip:
1925
2026::
2127
22- $ pip install git+git://github.com/tomas-fp/python-jsonpath-ng.git#egg= jsonpath-ng
28+ $ pip install --upgrade jsonpath-ng
2329
2430Then:
2531
5662 >> > jsonpath_expr_direct = Fields(' foo' ).child(Slice(' *' )).child(Fields(' baz' )) # This is equivalent
5763
5864
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+
5977 JSONPath Syntax
6078---------------
6179
@@ -144,8 +162,9 @@ easy to do so directly, and here are some examples:
144162- ``Where(Slice(), Fields('subfield')) ``
145163- ``Descendants(jsonpath, jsonpath) ``
146164
147- Extensions
148- ----------
165+
166+ Extras
167+ ------
149168
150169- *Path data *: The result of ``JsonPath.find `` provide detailed context
151170 and path data so it is easy to traverse to parent objects, print full
@@ -160,6 +179,69 @@ Extensions
160179 contained in backquotes can be made to be a new operator, currently
161180 by extending the library.
162181
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+
163245More to explore
164246---------------
165247
@@ -211,6 +293,7 @@ Copyright and License
211293---------------------
212294
213295Copyright 2013 - Kenneth Knowles
296+ Copyright 2017 - Tomas Aparicio
214297
215298Licensed under the Apache License, Version 2.0 (the "License"); you may
216299not 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
227310limitations under the License.
228311
229312.. _`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+
230316.. |Build Status | image :: https://travis-ci.org/kennknowles/python-jsonpath-ng.png?branch=master
231317 :target: https://travis-ci.org/kennknowles/python-jsonpath-ng
232318.. |Test coverage | image :: https://coveralls.io/repos/kennknowles/python-jsonpath-ng/badge.png?branch=master
0 commit comments