python的jsonpath的一个健壮且显著扩展的实现,具有用于元编程的清晰ast。

jsonpath-rw的Python项目详细描述


https://github.com/kennknowles/python-jsonpath-rw

Build StatusTest coveragePyPi versionPyPi downloads

这个库提供了一个健壮且显著扩展的实现 对于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,因为通过这些更改,语言不是 微不足道的联想。此外,字段可以被引用,不管它们是否 包含在括号中。

原子表达式:

SyntaxMeaning
^{tt3}$The root object
^{tt4}$The “current” object.
^{tt5}$More generally, this syntax allows “named operators” to extend JSONPath is arbitrary ways
fieldSpecified 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运算符:

SyntaxMeaning
jsonpath1 ^{tt10}$ jsonpath2All nodes matched by jsonpath2 starting at any node matching jsonpath1
jsonpath ^{tt6}$ whatever ^{tt7}$Same as jsonpath^{tt10}$whatever
jsonpath1 ^{tt14}$ jsonpath2All nodes matched by jsonpath2 that descend from any node matching jsonpath1
jsonpath1 ^{tt2}$ jsonpath2Any nodes matching jsonpath1 with a child matching jsonpath2
jsonpath1 ^{tt1}$ jsonpath2Any nodes matching the union of jsonpath1 and jsonpath2

字段说明符(field):

SyntaxMeaning
^{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}$ fieldeither of the named fields (you can always build equivalent jsonpath using ^{tt1}$)

数组说明符(idx):

SyntaxMeaning
^{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最简单的就是 不是很复杂吗,所以你可以用它们中的任何一个 成功。为什么不是这个?

据我所知,最初的提议是:

关于ply和docstrings的特别说明

这个库的主要解析工具包, PLY,不适用于docstring 远离的。例如,PYTHONOPTIMIZE=2python -OO都将 导致失败。

贡献者

此软件包由以下人员编写和维护:

借助于these contributors提交的修补程序。

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

推荐PyPI第三方库


热门话题
java在通配符泛型类型和泛型类型之间未经检查的强制转换   java Eclipse SWT Hello world教程:无法将显示解析为类型   java如何向远程用户发送注销消息?   java RDD之后的空文件是什么。保存ASTEXTFILE?   用户界面在java中创建一个htmljs UI GCalendar   Java多个哈希映射指向同一个键   Java Dowhile循环不工作?   oraclejava类。组织。阿帕奇。梁sdk。util。UserCodeException:java。sql。SQLException:无法创建PoolableConnectionFactory   java是org类型。日食用户界面。文本编辑器。*看不见   java有没有从弹出窗口复制eclipse中变量值的插件或快捷方式?   java getSubimage为我提供了期望值null   java我想让它变得更简单   swing AWTEventQueue0一直在运行,java中的程序变得非常慢   java Solr实例化类时出错:自定义类   java将ListView适配器移植到RecyclerView适配器   c#测试混合web和桌面应用程序的安全性