METADATA 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. Metadata-Version: 2.1
  2. Name: jmespath
  3. Version: 1.0.1
  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 :: 3
  16. Classifier: Programming Language :: Python :: 3.7
  17. Classifier: Programming Language :: Python :: 3.8
  18. Classifier: Programming Language :: Python :: 3.9
  19. Classifier: Programming Language :: Python :: 3.10
  20. Classifier: Programming Language :: Python :: 3.11
  21. Classifier: Programming Language :: Python :: Implementation :: CPython
  22. Classifier: Programming Language :: Python :: Implementation :: PyPy
  23. Requires-Python: >=3.7
  24. JMESPath
  25. ========
  26. .. image:: https://badges.gitter.im/Join Chat.svg
  27. :target: https://gitter.im/jmespath/chat
  28. JMESPath (pronounced "james path") allows you to declaratively specify how to
  29. extract elements from a JSON document.
  30. For example, given this document::
  31. {"foo": {"bar": "baz"}}
  32. The jmespath expression ``foo.bar`` will return "baz".
  33. JMESPath also supports:
  34. Referencing elements in a list. Given the data::
  35. {"foo": {"bar": ["one", "two"]}}
  36. The expression: ``foo.bar[0]`` will return "one".
  37. You can also reference all the items in a list using the ``*``
  38. syntax::
  39. {"foo": {"bar": [{"name": "one"}, {"name": "two"}]}}
  40. The expression: ``foo.bar[*].name`` will return ["one", "two"].
  41. Negative indexing is also supported (-1 refers to the last element
  42. in the list). Given the data above, the expression
  43. ``foo.bar[-1].name`` will return "two".
  44. The ``*`` can also be used for hash types::
  45. {"foo": {"bar": {"name": "one"}, "baz": {"name": "two"}}}
  46. The expression: ``foo.*.name`` will return ["one", "two"].
  47. Installation
  48. ============
  49. You can install JMESPath from pypi with:
  50. .. code:: bash
  51. pip install jmespath
  52. API
  53. ===
  54. The ``jmespath.py`` library has two functions
  55. that operate on python data structures. You can use ``search``
  56. and give it the jmespath expression and the data:
  57. .. code:: python
  58. >>> import jmespath
  59. >>> path = jmespath.search('foo.bar', {'foo': {'bar': 'baz'}})
  60. 'baz'
  61. Similar to the ``re`` module, you can use the ``compile`` function
  62. to compile the JMESPath expression and use this parsed expression
  63. to perform repeated searches:
  64. .. code:: python
  65. >>> import jmespath
  66. >>> expression = jmespath.compile('foo.bar')
  67. >>> expression.search({'foo': {'bar': 'baz'}})
  68. 'baz'
  69. >>> expression.search({'foo': {'bar': 'other'}})
  70. 'other'
  71. This is useful if you're going to use the same jmespath expression to
  72. search multiple documents. This avoids having to reparse the
  73. JMESPath expression each time you search a new document.
  74. Options
  75. -------
  76. You can provide an instance of ``jmespath.Options`` to control how
  77. a JMESPath expression is evaluated. The most common scenario for
  78. using an ``Options`` instance is if you want to have ordered output
  79. of your dict keys. To do this you can use either of these options:
  80. .. code:: python
  81. >>> import jmespath
  82. >>> jmespath.search('{a: a, b: b}',
  83. ... mydata,
  84. ... jmespath.Options(dict_cls=collections.OrderedDict))
  85. >>> import jmespath
  86. >>> parsed = jmespath.compile('{a: a, b: b}')
  87. >>> parsed.search(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.