跳转到主要内容

JSONPath RW扩展

项目描述

https://travis-ci.org/sileht/python-jsonpath-rw-ext.png?branch=master Latest Version Downloads

JSONPath RW扩展

jsonpath-rw-ext通过添加多个扩展来扩展json-path-rw的功能。例如,'len'允许获取列表的长度,'sorted'返回排序后的列表,'arithmetic'允许在元素之间进行数学运算,以及'filter'用于选择列表中的某些元素。

每个扩展都将向上游提出,并且只有在被拒绝的情况下才会保留在这里。

快速入门

在命令行中

$ pip install jsonpath-rw-ext

或者,如果您已安装virtualenvwrapper

$ mkvirtualenv jsonpath-rw-ext
$ pip install jsonpath-rw-ext

使用以下命令替换jsonpath_rw解析器

import jsonpath_rw_ext
jsonpath_rw_ext.parse("$.foo").find(...)

或者

from jsonpath_rw_ext import parser
parser.ExtentedJsonPathParser().parse("$.foo").find(...)

获取匹配值仅的快捷函数

import jsonpath_rw_ext as jp
print jp.match('$.cow[*]', {'cow': ['foo', 'bar'], 'fish': 'foobar'})
# prints ['foo', 'bar']

print jp.match1('$.cow[*]', {'cow': ['foo', 'bar'], 'fish': 'foobar'})
# prints 'foo'

jsonpath类不是公共API的一部分,因为当它们在代码库上游实现时,其名称/结构可能会发生变化。只有语法不应该发生变化。

扩展

名称

示例

长度

  • $.objects.`len`

子集

  • $.field.`sub(/foo\\+(.*)/, \\1)`

分割

  • $.field.`split(+, 2, -1)`

  • $.field.`split(sep, segement, maxsplit)`

排序

  • $.objects.`sorted`

  • $.objects[some_field]

  • $.objects[some_field,/other_field]

过滤

  • $.objects[?(@some_field > 5)]

  • $.objects[?(some_field = “foobar”)]

  • $.objects[?(some_field ~ “regexp”)]

  • $.objects[?(some_field > 5 & other < 2)]

算术运算(-+*/)

  • $.foo + “_” + $.bar

  • $.foo * 12

  • $.objects[*].cow + $.objects[*].cat

关于算术和字符串

操作使用Python运算符进行,并允许Python允许的类型,如果由于类型不兼容而无法进行操作,则返回[]。

当使用运算符时,jsonpath必须完全定义,否则jsonpath-rw-ext无法知道表达式是字符串还是jsonpath字段,在这种情况下,它将选择字符串类型。

数据示例

{
    'cow': 'foo',
    'fish': 'bar'
}
cow + fish 返回 cowfish
$.cow + $.fish 返回 foobar
$.cow + “_” + $.fish 返回 foo_bar
$.cow + “_” + fish 返回 foo_fish

关于算术和列表

如果两个列表具有相同的大小,则可以对两个列表进行算术运算。

数据示例

{'objects': [
    {'cow': 2, 'cat': 3},
    {'cow': 4, 'cat': 6}
]}
$.objects[*].cow + $.objects[*].cat 返回 [6, 9]

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。

源分发

jsonpath-rw-ext-1.2.2.tar.gz (20.6 kB 查看哈希)

上传时间

构建分发

jsonpath_rw_ext-1.2.2-py2.py3-none-any.whl (19.2 kB 查看哈希)

上传时间 Python 2 Python 3

支持者:

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面