METADATA 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. Metadata-Version: 2.0
  2. Name: jsonpath-rw
  3. Version: 1.4.0
  4. Summary: A robust and significantly extended implementation of JSONPath for Python, with a clear AST for metaprogramming.
  5. Home-page: https://github.com/kennknowles/python-jsonpath-rw
  6. Author: Kenneth Knowles
  7. Author-email: kenn.knowles@gmail.com
  8. License: Apache 2.0
  9. Platform: UNKNOWN
  10. Classifier: Development Status :: 5 - Production/Stable
  11. Classifier: Intended Audience :: Developers
  12. Classifier: License :: OSI Approved :: Apache Software License
  13. Classifier: Programming Language :: Python :: 2
  14. Classifier: Programming Language :: Python :: 2.6
  15. Classifier: Programming Language :: Python :: 2.7
  16. Classifier: Programming Language :: Python :: 3
  17. Classifier: Programming Language :: Python :: 3.2
  18. Classifier: Programming Language :: Python :: 3.3
  19. Requires-Dist: decorator
  20. Requires-Dist: ply
  21. Requires-Dist: six
  22. Python JSONPath RW
  23. ==================
  24. https://github.com/kennknowles/python-jsonpath-rw
  25. |Build Status| |Test coverage| |PyPi version| |PyPi downloads|
  26. This library provides a robust and significantly extended implementation
  27. of JSONPath for Python. It is tested with Python 2.6, 2.7, 3.2, 3.3.
  28. *(On travis-ci there is a segfault when running the tests with pypy; I don't think the problem lies with this library)*.
  29. This library differs from other JSONPath implementations in that it is a
  30. full *language* implementation, meaning the JSONPath expressions are
  31. first class objects, easy to analyze, transform, parse, print, and
  32. extend. (You can also execute them :-)
  33. Quick Start
  34. -----------
  35. To install, use pip:
  36. ::
  37. $ pip install jsonpath-rw
  38. Then:
  39. .. code:: python
  40. $ python
  41. >>> from jsonpath_rw import jsonpath, parse
  42. # A robust parser, not just a regex. (Makes powerful extensions possible; see below)
  43. >>> jsonpath_expr = parse('foo[*].baz')
  44. # Extracting values is easy
  45. >>> [match.value for match in jsonpath_expr.find({'foo': [{'baz': 1}, {'baz': 2}]})]
  46. [1, 2]
  47. # Matches remember where they came from
  48. >>> [str(match.full_path) for match in jsonpath_expr.find({'foo': [{'baz': 1}, {'baz': 2}]})]
  49. ['foo.[0].baz', 'foo.[1].baz']
  50. # And this can be useful for automatically providing ids for bits of data that do not have them (currently a global switch)
  51. >>> jsonpath.auto_id_field = 'id'
  52. >>> [match.value for match in parse('foo[*].id').find({'foo': [{'id': 'bizzle'}, {'baz': 3}]})]
  53. ['foo.bizzle', 'foo.[1]']
  54. # A handy extension: named operators like `parent`
  55. >>> [match.value for match in parse('a.*.b.`parent`.c').find({'a': {'x': {'b': 1, 'c': 'number one'}, 'y': {'b': 2, 'c': 'number two'}}})]
  56. ['number two', 'number one']
  57. # You can also build expressions directly quite easily
  58. >>> from jsonpath_rw.jsonpath import Fields
  59. >>> from jsonpath_rw.jsonpath import Slice
  60. >>> jsonpath_expr_direct = Fields('foo').child(Slice('*')).child(Fields('baz')) # This is equivalent
  61. JSONPath Syntax
  62. ---------------
  63. The JSONPath syntax supported by this library includes some additional
  64. features and omits some problematic features (those that make it
  65. unportable). In particular, some new operators such as ``|`` and
  66. ``where`` are available, and parentheses are used for grouping not for
  67. callbacks into Python, since with these changes the language is not
  68. trivially associative. Also, fields may be quoted whether or not they
  69. are contained in brackets.
  70. Atomic expressions:
  71. +-----------------------+---------------------------------------------------------------------------------------------+
  72. | Syntax | Meaning |
  73. +=======================+=============================================================================================+
  74. | ``$`` | The root object |
  75. +-----------------------+---------------------------------------------------------------------------------------------+
  76. | ```this``` | The "current" object. |
  77. +-----------------------+---------------------------------------------------------------------------------------------+
  78. | ```foo``` | More generally, this syntax allows "named operators" to extend JSONPath is arbitrary ways |
  79. +-----------------------+---------------------------------------------------------------------------------------------+
  80. | *field* | Specified field(s), described below |
  81. +-----------------------+---------------------------------------------------------------------------------------------+
  82. | ``[`` *field* ``]`` | Same as *field* |
  83. +-----------------------+---------------------------------------------------------------------------------------------+
  84. | ``[`` *idx* ``]`` | Array access, described below (this is always unambiguous with field access) |
  85. +-----------------------+---------------------------------------------------------------------------------------------+
  86. Jsonpath operators:
  87. +-------------------------------------+------------------------------------------------------------------------------------+
  88. | Syntax | Meaning |
  89. +=====================================+====================================================================================+
  90. | *jsonpath1* ``.`` *jsonpath2* | All nodes matched by *jsonpath2* starting at any node matching *jsonpath1* |
  91. +-------------------------------------+------------------------------------------------------------------------------------+
  92. | *jsonpath* ``[`` *whatever* ``]`` | Same as *jsonpath*\ ``.``\ *whatever* |
  93. +-------------------------------------+------------------------------------------------------------------------------------+
  94. | *jsonpath1* ``..`` *jsonpath2* | All nodes matched by *jsonpath2* that descend from any node matching *jsonpath1* |
  95. +-------------------------------------+------------------------------------------------------------------------------------+
  96. | *jsonpath1* ``where`` *jsonpath2* | Any nodes matching *jsonpath1* with a child matching *jsonpath2* |
  97. +-------------------------------------+------------------------------------------------------------------------------------+
  98. | *jsonpath1* ``|`` *jsonpath2* | Any nodes matching the union of *jsonpath1* and *jsonpath2* |
  99. +-------------------------------------+------------------------------------------------------------------------------------+
  100. Field specifiers ( *field* ):
  101. +-------------------------+-------------------------------------------------------------------------------------+
  102. | Syntax | Meaning |
  103. +=========================+=====================================================================================+
  104. | ``fieldname`` | the field ``fieldname`` (from the "current" object) |
  105. +-------------------------+-------------------------------------------------------------------------------------+
  106. | ``"fieldname"`` | same as above, for allowing special characters in the fieldname |
  107. +-------------------------+-------------------------------------------------------------------------------------+
  108. | ``'fieldname'`` | ditto |
  109. +-------------------------+-------------------------------------------------------------------------------------+
  110. | ``*`` | any field |
  111. +-------------------------+-------------------------------------------------------------------------------------+
  112. | *field* ``,`` *field* | either of the named fields (you can always build equivalent jsonpath using ``|``) |
  113. +-------------------------+-------------------------------------------------------------------------------------+
  114. Array specifiers ( *idx* ):
  115. +-----------------------------------------+---------------------------------------------------------------------------------------+
  116. | Syntax | Meaning |
  117. +=========================================+=======================================================================================+
  118. | ``[``\ *n*\ ``]`` | array index (may be comma-separated list) |
  119. +-----------------------------------------+---------------------------------------------------------------------------------------+
  120. | ``[``\ *start*\ ``?:``\ *end*\ ``?]`` | array slicing (note that *step* is unimplemented only due to lack of need thus far) |
  121. +-----------------------------------------+---------------------------------------------------------------------------------------+
  122. | ``[*]`` | any array index |
  123. +-----------------------------------------+---------------------------------------------------------------------------------------+
  124. Programmatic JSONPath
  125. ---------------------
  126. If you are programming in Python and would like a more robust way to
  127. create JSONPath expressions that does not depend on a parser, it is very
  128. easy to do so directly, and here are some examples:
  129. - ``Root()``
  130. - ``Slice(start=0, end=None, step=None)``
  131. - ``Fields('foo', 'bar')``
  132. - ``Index(42)``
  133. - ``Child(Fields('foo'), Index(42))``
  134. - ``Where(Slice(), Fields('subfield'))``
  135. - ``Descendants(jsonpath, jsonpath)``
  136. Extensions
  137. ----------
  138. - *Path data*: The result of ``JsonPath.find`` provide detailed context
  139. and path data so it is easy to traverse to parent objects, print full
  140. paths to pieces of data, and generate automatic ids.
  141. - *Automatic Ids*: If you set ``jsonpath_rw.auto_id_field`` to a value
  142. other than None, then for any piece of data missing that field, it
  143. will be replaced by the JSONPath to it, giving automatic unique ids
  144. to any piece of data. These ids will take into account any ids
  145. already present as well.
  146. - *Named operators*: Instead of using ``@`` to reference the currently
  147. object, this library uses ```this```. In general, any string
  148. contained in backquotes can be made to be a new operator, currently
  149. by extending the library.
  150. More to explore
  151. ---------------
  152. There are way too many jsonpath implementations out there to discuss.
  153. Some are robust, some are toy projects that still work fine, some are
  154. exercises. There will undoubtedly be many more. This one is made for use
  155. in released, maintained code, and in particular for programmatic access
  156. to the abstract syntax and extension. But JSONPath at its simplest just
  157. isn't that complicated, so you can probably use any of them
  158. successfully. Why not this one?
  159. The original proposal, as far as I know:
  160. - `JSONPath - XPath for
  161. JSON <http://goessner.net/articles/JSONPath/>`__ by Stefan Goessner.
  162. Special note about PLY and docstrings
  163. -------------------------------------
  164. The main parsing toolkit underlying this library,
  165. `PLY <https://github.com/dabeaz/ply>`__, does not work with docstrings
  166. removed. For example, ``PYTHONOPTIMIZE=2`` and ``python -OO`` will both
  167. cause a failure.
  168. Contributors
  169. ------------
  170. This package is authored and maintained by:
  171. - `Kenn Knowles <https://github.com/kennknowles>`__
  172. (`@kennknowles <https://twitter.com/KennKnowles>`__)
  173. with the help of patches submitted by `these contributors <https://github.com/kennknowles/python-jsonpath-rw/graphs/contributors>`__.
  174. Copyright and License
  175. ---------------------
  176. Copyright 2013- Kenneth Knowles
  177. Licensed under the Apache License, Version 2.0 (the "License"); you may
  178. not use this file except in compliance with the License. You may obtain
  179. a copy of the License at
  180. ::
  181. http://www.apache.org/licenses/LICENSE-2.0
  182. Unless required by applicable law or agreed to in writing, software
  183. distributed under the License is distributed on an "AS IS" BASIS,
  184. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  185. See the License for the specific language governing permissions and
  186. limitations under the License.
  187. .. |Build Status| image:: https://travis-ci.org/kennknowles/python-jsonpath-rw.png?branch=master
  188. :target: https://travis-ci.org/kennknowles/python-jsonpath-rw
  189. .. |Test coverage| image:: https://coveralls.io/repos/kennknowles/python-jsonpath-rw/badge.png?branch=master
  190. :target: https://coveralls.io/r/kennknowles/python-jsonpath-rw
  191. .. |PyPi version| image:: https://pypip.in/v/jsonpath-rw/badge.png
  192. :target: https://pypi.python.org/pypi/jsonpath-rw
  193. .. |PyPi downloads| image:: https://pypip.in/d/jsonpath-rw/badge.png
  194. :target: https://pypi.python.org/pypi/jsonpath-rw