README.rst 6.8 KB

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