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 ClassNotFoundException   jackson Java代码,用于解析任何json文件,而不考虑其格式或键   java如何在Dynamo DB中更新保留关键字的值。错误:属性名称是保留关键字;保留关键字:数据   java我可以从哪里下载classes12。jdk7的jar文件   Java“找不到主类emeraldrandomizer.EmeraldRandomizerApp.Program将退出。”   未正确旋转AVL树,JAVA   使用绑定时,java无法隐藏进度条   在准备好的语句中连接字符串的java方法   如何使用org创建包含多个数组的json对象。json。java中的JSONObject包?   mysql Java SQL查询问题,结果集不包含任何内容   java如何向ioBuffer分配和检索无符号长值   带有导致性能问题的信号量的java Executor服务   java为一个干净的体系结构(分离的)后端和前端网站创建一个WAR文件   java如何帮助Tesseract读取这种质量的图像   java应该覆盖哪个异常方法?   java使用安卓。支持v4。应用程序。碎片和安卓。应用程序。相同活动中的片段   java如何在不强制转换的情况下分配属性?   使用Java的文件io Java语法检查器。util。堆栈