METADATA 7.7 KB

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