RFC 9535 - JSONPath:Python中的JSON查询表达式
项目描述
RFC 9535 JSONPath:Python中的JSON查询表达式
我们严格遵守RFC 9535,并对JSONPath兼容性测试套件进行了测试。
目录
安装
使用pip安装Python JSONPath RFC 9535
pip install jsonpath-rfc9535
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()
# ...
链接
- 变更日志: https://github.com/jg-rp/python-jsonpath-rfc9535/blob/main/CHANGELOG.md
- PyPi: https://pypi.ac.cn/project/jsonpath-rfc9535
- 源代码: https://github.com/jg-rp/python-jsonpath-rfc9535
- 问题跟踪器: https://github.com/jg-rp/python-jsonpath-rfc9535/issues
相关项目
- Python JSONPath - 另一个实现JSONPath的Python包,但具有额外的功能和定制选项。
- JSON P3 - 使用TypeScript实现的RFC 9535。
API
find
find(query: str, value: JSONValue) -> JSONPathNodeList
将JSONPath表达式query应用于value。 value可以是任意类型的,可能嵌套的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]'
JSONPathNodeList
是list
的子类,具有一些辅助方法。
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-rfc9535
在MIT许可下分发。
项目详情
下载文件
下载您平台上的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分布
构建分布
jsonpath_rfc9535-0.1.3.tar.gz的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 2ce27ff4b857c8de8664e6d5528c7f7869ac1575d74f2562510d1d2bcbb1b5fc |
|
MD5 | 6fa4db0183059095176fe13cdc66a7dc |
|
BLAKE2b-256 | 8ee6313d94d5f7d4ada29dbc31a0a833857f97ac6e225e807b91911c7488338b |
jsonpath_rfc9535-0.1.3-py3-none-any.whl的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 05f9014bb8cbbe28bf65f3ff3062b7a126965f6e5e17f9575a475aad9dadfba2 |
|
MD5 | 5e1fd096b0355cfd36f2228051d45b74 |
|
BLAKE2b-256 | 36e0fc5634309641ef592604746452ecdb53bd52cf48eb962fc043d0c47f8b30 |