METADATA 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. Metadata-Version: 2.0
  2. Name: jmespath
  3. Version: 0.10.0
  4. Summary: JSON Matching Expressions
  5. Home-page: https://github.com/jmespath/jmespath.py
  6. Author: James Saryerwinnie
  7. Author-email: js@jamesls.com
  8. License: MIT
  9. Platform: UNKNOWN
  10. Classifier: Development Status :: 5 - Production/Stable
  11. Classifier: Intended Audience :: Developers
  12. Classifier: Natural Language :: English
  13. Classifier: License :: OSI Approved :: MIT License
  14. Classifier: Programming Language :: Python
  15. Classifier: Programming Language :: Python :: 2
  16. Classifier: Programming Language :: Python :: 2.6
  17. Classifier: Programming Language :: Python :: 2.7
  18. Classifier: Programming Language :: Python :: 3
  19. Classifier: Programming Language :: Python :: 3.3
  20. Classifier: Programming Language :: Python :: 3.4
  21. Classifier: Programming Language :: Python :: 3.5
  22. Classifier: Programming Language :: Python :: 3.6
  23. Classifier: Programming Language :: Python :: 3.7
  24. Classifier: Programming Language :: Python :: Implementation :: CPython
  25. Classifier: Programming Language :: Python :: Implementation :: PyPy
  26. Requires-Python: >=2.6, !=3.0.*, !=3.1.*, !=3.2.*
  27. JMESPath
  28. ========
  29. .. image:: https://badges.gitter.im/Join Chat.svg
  30. :target: https://gitter.im/jmespath/chat
  31. .. image:: https://travis-ci.org/jmespath/jmespath.py.svg?branch=develop
  32. :target: https://travis-ci.org/jmespath/jmespath.py
  33. .. image:: https://codecov.io/github/jmespath/jmespath.py/coverage.svg?branch=develop
  34. :target: https://codecov.io/github/jmespath/jmespath.py?branch=develop
  35. JMESPath (pronounced "james path") allows you to declaratively specify how to
  36. extract elements from a JSON document.
  37. For example, given this document::
  38. {"foo": {"bar": "baz"}}
  39. The jmespath expression ``foo.bar`` will return "baz".
  40. JMESPath also supports:
  41. Referencing elements in a list. Given the data::
  42. {"foo": {"bar": ["one", "two"]}}
  43. The expression: ``foo.bar[0]`` will return "one".
  44. You can also reference all the items in a list using the ``*``
  45. syntax::
  46. {"foo": {"bar": [{"name": "one"}, {"name": "two"}]}}
  47. The expression: ``foo.bar[*].name`` will return ["one", "two"].
  48. Negative indexing is also supported (-1 refers to the last element
  49. in the list). Given the data above, the expression
  50. ``foo.bar[-1].name`` will return "two".
  51. The ``*`` can also be used for hash types::
  52. {"foo": {"bar": {"name": "one"}, "baz": {"name": "two"}}}
  53. The expression: ``foo.*.name`` will return ["one", "two"].
  54. Installation
  55. ============
  56. You can install JMESPath from pypi with:
  57. .. code:: bash
  58. pip install jmespath
  59. API
  60. ===
  61. The ``jmespath.py`` library has two functions
  62. that operate on python data structures. You can use ``search``
  63. and give it the jmespath expression and the data:
  64. .. code:: python
  65. >>> import jmespath
  66. >>> path = jmespath.search('foo.bar', {'foo': {'bar': 'baz'}})
  67. 'baz'
  68. Similar to the ``re`` module, you can use the ``compile`` function
  69. to compile the JMESPath expression and use this parsed expression
  70. to perform repeated searches:
  71. .. code:: python
  72. >>> import jmespath
  73. >>> expression = jmespath.compile('foo.bar')
  74. >>> expression.search({'foo': {'bar': 'baz'}})
  75. 'baz'
  76. >>> expression.search({'foo': {'bar': 'other'}})
  77. 'other'
  78. This is useful if you're going to use the same jmespath expression to
  79. search multiple documents. This avoids having to reparse the
  80. JMESPath expression each time you search a new document.
  81. Options
  82. -------
  83. You can provide an instance of ``jmespath.Options`` to control how
  84. a JMESPath expression is evaluated. The most common scenario for
  85. using an ``Options`` instance is if you want to have ordered output
  86. of your dict keys. To do this you can use either of these options:
  87. .. code:: python
  88. >>> import jmespath
  89. >>> jmespath.search('{a: a, b: b}',
  90. ... mydata,
  91. ... jmespath.Options(dict_cls=collections.OrderedDict))
  92. >>> import jmespath
  93. >>> parsed = jmespath.compile('{a: a, b: b}')
  94. >>> parsed.search(mydata,
  95. ... jmespath.Options(dict_cls=collections.OrderedDict))
  96. Custom Functions
  97. ~~~~~~~~~~~~~~~~
  98. The JMESPath language has numerous
  99. `built-in functions
  100. <http://jmespath.org/specification.html#built-in-functions>`__, but it is
  101. also possible to add your own custom functions. Keep in mind that
  102. custom function support in jmespath.py is experimental and the API may
  103. change based on feedback.
  104. **If you have a custom function that you've found useful, consider submitting
  105. it to jmespath.site and propose that it be added to the JMESPath language.**
  106. You can submit proposals
  107. `here <https://github.com/jmespath/jmespath.site/issues>`__.
  108. To create custom functions:
  109. * Create a subclass of ``jmespath.functions.Functions``.
  110. * Create a method with the name ``_func_<your function name>``.
  111. * Apply the ``jmespath.functions.signature`` decorator that indicates
  112. the expected types of the function arguments.
  113. * Provide an instance of your subclass in a ``jmespath.Options`` object.
  114. Below are a few examples:
  115. .. code:: python
  116. import jmespath
  117. from jmespath import functions
  118. # 1. Create a subclass of functions.Functions.
  119. # The function.Functions base class has logic
  120. # that introspects all of its methods and automatically
  121. # registers your custom functions in its function table.
  122. class CustomFunctions(functions.Functions):
  123. # 2 and 3. Create a function that starts with _func_
  124. # and decorate it with @signature which indicates its
  125. # expected types.
  126. # In this example, we're creating a jmespath function
  127. # called "unique_letters" that accepts a single argument
  128. # with an expected type "string".
  129. @functions.signature({'types': ['string']})
  130. def _func_unique_letters(self, s):
  131. # Given a string s, return a sorted
  132. # string of unique letters: 'ccbbadd' -> 'abcd'
  133. return ''.join(sorted(set(s)))
  134. # Here's another example. This is creating
  135. # a jmespath function called "my_add" that expects
  136. # two arguments, both of which should be of type number.
  137. @functions.signature({'types': ['number']}, {'types': ['number']})
  138. def _func_my_add(self, x, y):
  139. return x + y
  140. # 4. Provide an instance of your subclass in a Options object.
  141. options = jmespath.Options(custom_functions=CustomFunctions())
  142. # Provide this value to jmespath.search:
  143. # This will print 3
  144. print(
  145. jmespath.search(
  146. 'my_add(`1`, `2`)', {}, options=options)
  147. )
  148. # This will print "abcd"
  149. print(
  150. jmespath.search(
  151. 'foo.bar | unique_letters(@)',
  152. {'foo': {'bar': 'ccbbadd'}},
  153. options=options)
  154. )
  155. Again, if you come up with useful functions that you think make
  156. sense in the JMESPath language (and make sense to implement in all
  157. JMESPath libraries, not just python), please let us know at
  158. `jmespath.site <https://github.com/jmespath/jmespath.site/issues>`__.
  159. Specification
  160. =============
  161. If you'd like to learn more about the JMESPath language, you can check out
  162. the `JMESPath tutorial <http://jmespath.org/tutorial.html>`__. Also check
  163. out the `JMESPath examples page <http://jmespath.org/examples.html>`__ for
  164. examples of more complex jmespath queries.
  165. The grammar is specified using ABNF, as described in
  166. `RFC4234 <http://www.ietf.org/rfc/rfc4234.txt>`_.
  167. You can find the most up to date
  168. `grammar for JMESPath here <http://jmespath.org/specification.html#grammar>`__.
  169. You can read the full
  170. `JMESPath specification here <http://jmespath.org/specification.html>`__.
  171. Testing
  172. =======
  173. In addition to the unit tests for the jmespath modules,
  174. there is a ``tests/compliance`` directory that contains
  175. .json files with test cases. This allows other implementations
  176. to verify they are producing the correct output. Each json
  177. file is grouped by feature.
  178. Discuss
  179. =======
  180. Join us on our `Gitter channel <https://gitter.im/jmespath/chat>`__
  181. if you want to chat or if you have any questions.