You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
7.6 KiB

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