python的jsonpath的一个健壮且显著扩展的实现,具有用于元编程的清晰ast。
jsonpath-rw的Python项目详细描述
https://github.com/kennknowles/python-jsonpath-rw
这个库提供了一个健壮且显著扩展的实现 对于python的jsonpath。它是用Python2.6、2.7、3.2、3.3进行测试的。 (在travis ci上,使用pypy运行测试时出现segfault;我认为问题不在于此库)。
这个库不同于其他jsonpath实现,因为它是 完整的language实现,这意味着jsonpath表达式是 一类对象,易于分析、转换、分析、打印和 延伸。(您也可以执行它们:-)
快速启动
要安装,请使用pip:
$ pip install jsonpath-rw
然后:
$python>>>fromjsonpath_rwimportjsonpath,parse# A robust parser, not just a regex. (Makes powerful extensions possible; see below)>>>jsonpath_expr=parse('foo[*].baz')# Extracting values is easy>>>[match.valueformatchinjsonpath_expr.find({'foo':[{'baz':1},{'baz':2}]})][1,2]# Matches remember where they came from>>>[str(match.full_path)formatchinjsonpath_expr.find({'foo':[{'baz':1},{'baz':2}]})]['foo.[0].baz','foo.[1].baz']# And this can be useful for automatically providing ids for bits of data that do not have them (currently a global switch)>>>jsonpath.auto_id_field='id'>>>[match.valueformatchinparse('foo[*].id').find({'foo':[{'id':'bizzle'},{'baz':3}]})]['foo.bizzle','foo.[1]']# A handy extension: named operators like `parent`>>>[match.valueformatchinparse('a.*.b.`parent`.c').find({'a':{'x':{'b':1,'c':'number one'},'y':{'b':2,'c':'number two'}}})]['number two','number one']# You can also build expressions directly quite easily>>>fromjsonpath_rw.jsonpathimportFields>>>fromjsonpath_rw.jsonpathimportSlice>>>jsonpath_expr_direct=Fields('foo').child(Slice('*')).child(Fields('baz'))# This is equivalent
jsonpath语法
这个库支持的jsonpath语法包括一些额外的 特性并省略一些有问题的特性(使 不可携带)。特别是一些新的运算符,如|和 where可用,括号用于分组而不是 回调到python,因为通过这些更改,语言不是 微不足道的联想。此外,字段可以被引用,不管它们是否 包含在括号中。
原子表达式:
Syntax | Meaning |
---|---|
^{tt3}$ | The root object |
^{tt4}$ | The “current” object. |
^{tt5}$ | More generally, this syntax allows “named operators” to extend JSONPath is arbitrary ways |
field | Specified field(s), described below |
^{tt6}$ field ^{tt7}$ | Same as field |
^{tt6}$ idx ^{tt7}$ | Array access, described below (this is always unambiguous with field access) |
jsonpath运算符:
Syntax | Meaning |
---|---|
jsonpath1 ^{tt10}$ jsonpath2 | All nodes matched by jsonpath2 starting at any node matching jsonpath1 |
jsonpath ^{tt6}$ whatever ^{tt7}$ | Same as jsonpath^{tt10}$whatever |
jsonpath1 ^{tt14}$ jsonpath2 | All nodes matched by jsonpath2 that descend from any node matching jsonpath1 |
jsonpath1 ^{tt2}$ jsonpath2 | Any nodes matching jsonpath1 with a child matching jsonpath2 |
jsonpath1 ^{tt1}$ jsonpath2 | Any nodes matching the union of jsonpath1 and jsonpath2 |
字段说明符(field):
Syntax | Meaning |
---|---|
^{tt17}$ | the field ^{tt17}$ (from the “current” object) |
^{tt19}$ | same as above, for allowing special characters in the fieldname |
^{tt20}$ | ditto |
^{tt21}$ | any field |
field ^{tt22}$ field | either of the named fields (you can always build equivalent jsonpath using ^{tt1}$) |
数组说明符(idx):
Syntax | Meaning |
---|---|
^{tt6}$n^{tt7}$ | array index (may be comma-separated list) |
^{tt6}$start^{tt27}$end^{tt28}$ | array slicing (note that step is unimplemented only due to lack of need thus far) |
^{tt29}$ | any array index |
编程jsonpath
如果您是用python编程的,并且希望使用更健壮的方法 创建不依赖于解析器的jsonpath表达式,它非常 很容易直接这样做,下面是一些示例:
- Root()
- Slice(start=0, end=None, step=None)
- Fields('foo', 'bar')
- Index(42)
- Child(Fields('foo'), Index(42))
- Where(Slice(),Fields('subfield'))
- Descendants(jsonpath, jsonpath)
扩展名
- path data:结果JsonPath.findprovide detailed context 和路径数据,以便轻松遍历到父对象,打印完整 指向数据片段的路径,并生成自动ID。
- 自动id:如果将jsonpath_rw.auto_id_field设置为值 除了“无”之外,对于任何缺少该字段的数据块, 将被它的jsonpath替换,并提供自动唯一的id 任何数据。这些ID将考虑任何ID 也已经出现了。
- 命名运算符:而不是使用@引用当前 对象,此库使用`this`。一般来说,任何字符串 包含在反引号中的可以成为一个新的运算符,当前 通过扩展库。
更多探索内容
有太多的jsonpath实现需要讨论。 一些是强大的,一些是玩具项目,仍然运作良好,一些是 练习。毫无疑问还会有更多。这个是为使用而做的 在已发布、维护的代码中,特别是在编程访问中 抽象语法和扩展。但是jsonpath最简单的就是 不是很复杂吗,所以你可以用它们中的任何一个 成功。为什么不是这个?
据我所知,最初的提议是:
- JSONPath - XPath for JSON作者stefan goessner。
关于ply和docstrings的特别说明
这个库的主要解析工具包, PLY,不适用于docstring 远离的。例如,PYTHONOPTIMIZE=2和python -OO都将 导致失败。
版权和许可
版权所有2013-Kenneth Knowles
根据apache许可证2.0版(以下简称“许可证”)授权;您可以 除非符合许可证,否则不要使用此文件。你可以获得 在
http://www.apache.org/licenses/LICENSE-2.0
除非适用法律要求或书面同意,否则软件 根据许可证分发的是是“基础, 无任何明示或默示的保证或条件。 有关管理权限的特定语言和 许可下的限制。