跳转到主要内容

RFC 9535 - JSONPath:Python中的JSON查询表达式

项目描述

RFC 9535 JSONPath:Python中的JSON查询表达式

我们严格遵守RFC 9535,并对JSONPath兼容性测试套件进行了测试。

License Tests
PyPi - Version Python versions


目录

安装

使用pip安装Python JSONPath RFC 9535

pip install jsonpath-rfc9535

Pipenv

pipenv install -u jsonpath-rfc9535

示例

import jsonpath_rfc9535 as jsonpath

data = {
    "users": [
        {"name": "Sue", "score": 100},
        {"name": "Sally", "score": 84, "admin": False},
        {"name": "John", "score": 86, "admin": True},
        {"name": "Jane", "score": 55},
    ],
    "moderator": "John",
}

for node in jsonpath.find("$.users[?@.score > 85]", data):
    print(node.value)

# {'name': 'Sue', 'score': 100}
# {'name': 'John', 'score': 86, 'admin': True}

或,从文件中读取JSON数据

import json
import jsonpath_rfc9535 as jsonpath

with open("/path/to/some.json", encoding="utf-8") as fd:
    data = json.load(fd)

nodes = jsonpath.find("$.some.query", data)
values = nodes.values()
# ...

您还可以从YAML格式化的文件中读取数据。如果您已安装PyYaml

import jsonpath_rfc9535 as jsonpath
import yaml

with open("some.yaml") as fd:
    data = yaml.safe_load(fd)

products = jsonpath.find("$..products.*", data).values()
# ...

链接

相关项目

  • Python JSONPath - 另一个实现JSONPath的Python包,但具有额外的功能和定制选项。
  • JSON P3 - 使用TypeScript实现的RFC 9535。

API

find

find(query: str, value: JSONValue) -> JSONPathNodeList

将JSONPath表达式query应用于valuevalue可以是任意类型的,可能嵌套的Python字典、列表、字符串、整数、浮点数、布尔值或None,就像从json.load()获得的。

返回一个JSONPathNode实例列表,每个节点对应一个与路径匹配的值。如果没有匹配项,返回的列表将为空。

每个JSONPathNode具有

  • 一个value属性,它是与节点关联的类似JSON的值。
  • 一个location属性,它是一个元组,包含到达目标JSON文档中节点值的所需属性名和数组/列表索引。
  • 一个path()方法,它返回目标JSON文档中节点的标准化路径。
import jsonpath_rfc9535 as jsonpath

value = {
    "users": [
        {"name": "Sue", "score": 100},
        {"name": "John", "score": 86, "admin": True},
        {"name": "Sally", "score": 84, "admin": False},
        {"name": "Jane", "score": 55},
    ],
    "moderator": "John",
}

for node in jsonpath.find("$.users[?@.score > 85]", value):
    print(f"{node.value} at '{node.path()}'")

# {'name': 'Sue', 'score': 100} at '$['users'][0]'
# {'name': 'John', 'score': 86, 'admin': True} at '$['users'][1]'

JSONPathNodeListlist的子类,具有一些辅助方法。

  • values()返回一个值列表,每个节点对应一个值。
  • items()返回一个(标准化路径,值)元组列表。

find_one

find_one(query: str, value: JSONValue) -> Optional[JSONPathNode]

find_one()接受与find()相同的参数,但返回第一个可用的JSONPathNode,如果没有匹配项则返回None

find_one()等价于

def find_one(query, value):
    try:
        return next(iter(jsonpath.finditer(query, value)))
    except StopIteration:
        return None

finditer

finditer(query: str, value: JSONValue) -> Iterable[JSONPathNode]

finditer()接受与find()相同的参数,但它返回一个遍历JSONPathNode实例的迭代器,而不是列表。如果您期望大量结果且不想一次性将它们全部加载到内存中,这可能很有用。

compile

compile(query: str) -> JSONPathQuery

find(query, value)JSONPathEnvironment().compile(query).apply(value)的便利函数。使用compile(query)获取一个可以重复应用于不同类似JSON值的JSONPathQuery实例。

import jsonpath_rfc9535 as jsonpath

value = {
    "users": [
        {"name": "Sue", "score": 100},
        {"name": "John", "score": 86, "admin": True},
        {"name": "Sally", "score": 84, "admin": False},
        {"name": "Jane", "score": 55},
    ],
    "moderator": "John",
}

query = jsonpath.compile("$.users[?@.score > 85]")

for node in query.apply(value):
    print(f"{node.value} at '{node.path()}'")

# {'name': 'Sue', 'score': 100} at '$['users'][0]'
# {'name': 'John', 'score': 86, 'admin': True} at '$['users'][1]'

JSONPathQuery也有一个finditer(value)方法,而find(value)apply(value)的别名。

许可证

python-jsonpath-rfc9535MIT许可下分发。

项目详情


下载文件

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

源分布

jsonpath_rfc9535-0.1.3.tar.gz (26.0 kB 查看哈希)

上传时间

构建分布

jsonpath_rfc9535-0.1.3-py3-none-any.whl (34.9 kB 查看哈希)

上传时间 Python 3

支持者

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