JMESPath is a query language for JSON.
| Files | Last commit | Last update |
|---|---|---|
| 11 months ago | ||
| 6 years ago | ||
| 4 years ago | ||
| 9 months ago | ||
| 10 years ago | ||
| 3 years ago | ||
| 11 months ago | ||
| 10 years ago | ||
| 10 years ago | ||
| 11 years ago | ||
| 11 months ago | ||
| 3 years ago | ||
| 3 years ago | ||
| 4 years ago | ||
| 11 months ago | ||
| 3 years ago | ||
| 9 months ago | ||
| 11 months ago |
JMESPath
.. image:: https://badges.gitter.im/Join Chat.svg :target: https://gitter.im/jmespath/chat
JMESPath(发音为“james path”)允许您声明性地指定如何从 JSON 文档中提取元素。
例如,给定以下文档:
{"foo": {"bar": "baz"}}
JMESPath 表达式 foo.bar 将返回 "baz"。
JMESPath 还支持:
在列表中引用元素。给定以下数据:
{"foo": {"bar": ["one", "two"]}}
表达式:foo.bar[0] 将返回 "one"。您还可以使用 * 语法引用列表中的所有项目:
{"foo": {"bar": [{"name": "one"}, {"name": "two"}]}}
表达式:foo.bar[*].name 将返回 ["one", "two"]。负索引也受支持(-1 指的是列表中的最后一个元素)。根据上述数据,表达式 foo.bar[-1].name 将返回 "two"。
* 也可以用于哈希类型:
{"foo": {"bar": {"name": "one"}, "baz": {"name": "two"}}}
表达式:foo.*.name 将返回 ["one", "two"]。
安装
您可以从 PyPI 安装 JMESPath:
pip install jmespath
API
jmespath.py 库有两个操作 Python 数据结构的功能。您可以使用 search 并为其提供 JMESPath 表达式和数据:
>>> import jmespath
>>> path = jmespath.search('foo.bar', {'foo': {'bar': 'baz'}})
'baz'
类似于 re 模块,您可以使用 compile 函数编译 JMESPath 表达式,并使用这个解析过的表达式执行重复搜索:
>>> import jmespath
>>> expression = jmespath.compile('foo.bar')
>>> expression.search({'foo': {'bar': 'baz'}})
'baz'
>>> expression.search({'foo': {'bar': 'other'}})
'other'
如果您打算用相同的 JMESPath 表达式搜索多个文档,这将非常有用。这样可以避免每次搜索新文档时重新解析 JMESPath 表达式。
选项
您可以提供一个 jmespath.Options 实例来控制如何评估 JMESPath 表达式。使用 Options 实例的最常见场景是如果您想要对字典键进行有序输出。为此,您可以使用以下任一选项:
>>> import jmespath
>>> jmespath.search('{a: a, b: b}',
... mydata,
... jmespath.Options(dict_cls=collections.OrderedDict))
>>> import jmespath
>>> parsed = jmespath.compile('{a: a, b: b}')
>>> parsed.search(mydata,
... jmespath.Options(dict_cls=collections.OrderedDict))
自定义函数
The JMESPath language has numerous
`built-in functions
<http://jmespath.org/specification.html#built-in-functions>`__, but it is
also possible to add your own custom functions. Keep in mind that
custom function support in jmespath.py is experimental and the API may
change based on feedback.
**If you have a custom function that you've found useful, consider submitting
it to jmespath.site and propose that it be added to the JMESPath language.**
You can submit proposals
[here](https://github.com/jmespath/jmespath.site/issues).
To create custom functions:
* Create a subclass of ``jmespath.functions.Functions``.
* Create a method with the name ``_func_<your function name>``.
* Apply the ``jmespath.functions.signature`` decorator that indicates
the expected types of the function arguments.
* Provide an instance of your subclass in a ``jmespath.Options`` object.
Below are a few examples:
.. code:: python
import jmespath
from jmespath import functions
# 1. Create a subclass of functions.Functions.
# The function.Functions base class has logic
# that introspects all of its methods and automatically
# registers your custom functions in its function table.
class CustomFunctions(functions.Functions):
# 2 and 3. Create a function that starts with _func_
# and decorate it with @signature which indicates its
# expected types.
# In this example, we're creating a jmespath function
# called "unique_letters" that accepts a single argument
# with an expected type "string".
@functions.signature({'types': ['string']})
def _func_unique_letters(self, s):
# Given a string s, return a sorted
# string of unique letters: 'ccbbadd' -> 'abcd'
return ''.join(sorted(set(s)))
# Here's another example. This is creating
# a jmespath function called "my_add" that expects
# two arguments, both of which should be of type number.
@functions.signature({'types': ['number']}, {'types': ['number']})
def _func_my_add(self, x, y):
return x + y
# 4. Provide an instance of your subclass in a Options object.
options = jmespath.Options(custom_functions=CustomFunctions())
# Provide this value to jmespath.search:
# This will print 3
print(
jmespath.search(
'my_add(`1`, `2`)', {}, options=options)
)
# This will print "abcd"
print(
jmespath.search(
'foo.bar | unique_letters(@)',
{'foo': {'bar': 'ccbbadd'}},
options=options)
)
Again, if you come up with useful functions that you think make
sense in the JMESPath language (and make sense to implement in all
JMESPath libraries, not just python), please let us know at
[jmespath.site](https://github.com/jmespath/jmespath.site/issues).
Specification
=============
If you'd like to learn more about the JMESPath language, you can check out
the [JMESPath tutorial](http://jmespath.org/tutorial.html). Also check
out the [JMESPath examples page](http://jmespath.org/examples.html) for
examples of more complex jmespath queries.
The grammar is specified using ABNF, as described in
[RFC4234](http://www.ietf.org/rfc/rfc4234.txt).
You can find the most up to date
[grammar for JMESPath here](http://jmespath.org/specification.html#grammar).
You can read the full
[JMESPath specification here](http://jmespath.org/specification.html).
Testing
=======
In addition to the unit tests for the jmespath modules,
there is a ``tests/compliance`` directory that contains
.json files with test cases. This allows other implementations
to verify they are producing the correct output. Each json
file is grouped by feature.
Discuss
=======
Join us on our [Gitter channel](https://gitter.im/jmespath/chat)
if you want to chat or if you have any questions.
当然,我可以帮助您翻译文本。不过,您需要提供需要翻译的具体文本内容。请提供原文,我将会按照您的要求进行翻译。