python 操作 jsonpath
一、简介
jsonpath-ng 和 jsonpath-rw 两个用于解析 JSONPath 表达式的 Python 库
jsonpath-ng
- 基本 JSONPath 语法(如根节点、子节点、通配符、数组索引、过滤表达式等)。
- 不支持过滤器中的正则表达式和脚本表达式。
- 不支持某些复杂的逻辑运算。
jsonpath-rw
- 更广泛的 JSONPath 语法,包括过滤器中的正则表达式和脚本表达式。
- 支持更复杂的逻辑运算和嵌套的过滤条件。
- 支持递归下降操作符 ..。
二、安装
pip install jsonpath-rw -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
pip install jsonpath-ng -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
三、基本用法
3.1 获取所有书籍的标题:
from jsonpath_ng import jsonpath, parse
data = {
"store": {
"book": [
{"category": "fiction", "title": "西游记", "author": "F. Scott Fitzgerald", "price": 10.99},
{"category": "non-fiction", "title": "红楼梦", "author": "Yuval Noah Harari", "price": 15.99}
],
"bicycle": {"color": "red", "price": 19.99}
}
}
jsonpath_expression = parse('$.store.book[*].title')
results = [match.value for match in jsonpath_expression.find(data)]
print(results)
from jsonpath_rw import jsonpath, parse
data = {
"store": {
"book": [
{"category": "fiction", "title": "西游记", "author": "F. Scott Fitzgerald", "price": 10.99},
{"category": "non-fiction", "title": "红楼梦", "author": "Yuval Noah Harari", "price": 15.99}
],
"bicycle": {"color": "red", "price": 19.99}
}
}
jsonpath_expression = parse('$.store.book[*].title')
results = [match.value for match in jsonpath_expression.find(data)]
print(results)
3.2 获取所有书籍的价格:
jsonpath_expression = parse('$.store.book[*].price')
results = [match.value for match in jsonpath_expression.find(data)]
print(results)
3.3 获取某一类别的书籍:
jsonpath_expression = parse('$.store.book[?(@.category == "fiction")]')
results = [match.value for match in jsonpath_expression.find(data)]
print(results)
3.4 获取自行车的颜色:
jsonpath_expression = parse('$.store.bicycle.color')
results = [match.value for match in jsonpath_expression.find(data)][0]
print(results)
四、处理嵌套结构
4.1 获取所有商店的名字:
from jsonpath_ng import jsonpath, parse
data = {
"stores": [
{
"name": "张三",
"items": [
{"type": "book", "title": "红楼梦", "price": 10.99},
{"type": "toy", "name": "1", "price": 5.99}
]
},
{
"name": "李四",
"items": [
{"type": "book", "title": "西游记", "price": 12.99},
{"type": "toy", "name": "2", "price": 6.99}
]
}
]
}
jsonpath_expression = parse('$.stores[*].name')
results = [match.value for match in jsonpath_expression.find(data)]
print(results)
from jsonpath_rw import jsonpath, parse
data = {
"stores": [
{
"name": "张三",
"items": [
{"type": "book", "title": "红楼梦", "price": 10.99},
{"type": "toy", "name": "1", "price": 5.99}
]
},
{
"name": "李四",
"items": [
{"type": "book", "title": "西游记", "price": 12.99},
{"type": "toy", "name": "2", "price": 6.99}
]
}
]
}
jsonpath_expression = parse('$.stores[*].name')
results = [match.value for match in jsonpath_expression.find(data)]
print(results)
4.2 获取所有书的标题和价格:
jsonpath_expression = parse('$.stores[*].items[?(@.type == "book")].{title: title, price: price}')
results = [match.value for match in jsonpath_expression.find(data)]
print(results)
五、高级用法
1. 过滤条件
获取价格大于 10 的书籍:
jsonpath_expression = parse('$.store.book[?(@.price > 10)]')
results = [match.value for match in jsonpath_expression.find(data)]
print(results)
2. 投影(只返回指定的字段。这对于减少返回数据的大小非常有用)
jsonpath_expression = parse('$.store.book[*].{title: title, author: author}')
results = [match.value for match in jsonpath_expression.find(data)]
print(results)
3. 组合查询
jsonpath_expression = parse('$.store.book[?(@.category == "non-fiction")].{title: title, price: price}')
results = [match.value for match in jsonpath_expression.find(data)]
print(results)
4. 处理数组索引
jsonpath_expression = parse('$.stores[0].items[0]')
results = [match.value for match in jsonpath_expression.find(data)]
print(results)
五、错误处理
可以使用 try-except 语句来捕获潜在的异常:
try:
jsonpath_expression = parse('$.store.book[*].title')
results = [match.value for match in jsonpath_expression.find(data)]
print(results)
except Exception as e:
print(f"An error occurred:{e}")

浙公网安备 33010602011771号