JSON匹配表达式

jmespath的Python项目详细描述


杰梅斯帕思

https://badges.gitter.im/JoinChat.svghttps://travis-ci.org/jmespath/jmespath.py.svg?branch=develophttps://codecov.io/github/jmespath/jmespath.py/coverage.svg?branch=develop

JMESPath(发音为“james path”)允许您声明性地指定如何 从JSON文档中提取元素

例如,给定此文档:

{"foo": {"bar": "baz"}}

jmespath表达式foo.bar将返回“baz”。

jmespath还支持:

引用列表中的元素根据数据:

{"foo": {"bar": ["one", "two"]}}

表达式:foo.bar[0]将返回“one”。 还可以使用^{tt3}引用列表中的所有项。$ 语法:

{"foo": {"bar": [{"name": "one"}, {"name": "two"}]}}

表达式:foo.bar[*].name将返回[“one”,“two”]。 还支持负索引(-1)引用最后一个元素 在列表中)根据上面的数据,表达式 foo.bar[-1].name将返回“2”。

*也可用于哈希类型:

{"foo": {"bar": {"name": "one"}, "baz": {"name": "two"}}}

表达式:foo.*.name将返回[“one”,“two”]。

API

jmespath.py库有两个函数 在python数据结构上操作的。您可以使用search 并给出jmespath表达式和数据:

>>>importjmespath>>>path=jmespath.search('foo.bar',{'foo':{'bar':'baz'}})'baz'

类似于re模块,您可以使用compile函数 编译JMESPath表达式并使用此解析表达式 执行重复搜索:

>>>importjmespath>>>expression=jmespath.compile('foo.bar')>>>expression.search({'foo':{'bar':'baz'}})'baz'>>>expression.search({'foo':{'bar':'other'}})'other'

如果要使用相同的jmespath表达式 搜索多个文档。这样可以避免重新分析 每次搜索新文档时都使用JMESPath表达式

选项

您可以提供jmespath.Options的实例来控制 对JMESPath表达式求值最常见的场景 使用Options实例是指如果希望有有序的输出 你的dict键。为此,您可以使用以下任一选项:

>>>importjmespath>>>jmespath.search('{a: a, b: b}',...mydata,...jmespath.Options(dict_cls=collections.OrderedDict))>>>importjmespath>>>parsed=jmespath.compile('{a: a, b: b}')>>>parsed.search(mydata,...jmespath.Options(dict_cls=collections.OrderedDict))

自定义函数

jmespath语言有许多 built-in functions,但它是 也可以添加自己的自定义函数。记住 py中的自定义函数支持是实验性的,api可以 根据反馈进行更改。

如果您有一个您认为有用的自定义函数,请考虑提交 发送到jmespath.site并建议将其添加到jmespath语言中。 你可以提交建议 here

要创建自定义函数:

  • 创建jmespath.functions.Functions的子类。
  • 创建名为_func_<your function name>的方法
  • 应用jmespath.functions.signaturedecorator来指示 函数参数的预期类型
  • jmespath.Options对象中提供子类的实例。

下面是几个示例:

importjmespathfromjmespathimportfunctions# 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.classCustomFunctions(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):returnx+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 3print(jmespath.search('my_add(`1`, `2`)',{},options=options))# This will print "abcd"print(jmespath.search('foo.bar | unique_letters(@)',{'foo':{'bar':'ccbbadd'}},options=options))

再说一次,如果你想出有用的函数 在jmespath语言中有意义(并且在所有 JMESPath库,不仅仅是python),请在 jmespath.site

规格

如果您想了解更多关于jmespath语言的信息,可以查看 那张JMESPath tutorial。也检查 出JMESPath examples page的 更复杂的jmespath查询的示例。

语法使用ABNF指定,如中所述 RFC4234。 你可以找到最新的 grammar for JMESPath here

你可以看全文 JMESPath specification here

测试

除了jmespath模块的单元测试之外, 有一个tests/compliance目录包含 .json文件和测试用例。这允许其他实现 以验证它们产生的输出是否正确。每个json 文件按功能分组。

讨论

加入我们的Gitter channel 如果你想聊天或者有任何问题

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
IE中的java跨域cookie问题   重复java中已经满足的循环   编译java RMI服务器时出错   JavaServlet POST中作为参数传递的javascript大型JSON数组数据为空   java片段未每次刷新/调用   java无法编译。错误消息   java如何构造大型类?   java Hibernate:TableThingsDB。事情并不存在   java如何操作从匹配项创建的数组。发现   循环以搜索和显示数组Java的某些部分   加载或注册SQLite JDBC驱动程序时出现java问题   活动和服务之间的java连接   JavaGWTG2D:ie8中的drawImage   java在安卓中设置hessian阈值   在Tomcat中使用Logback时发生java错误