基于jsonpath的JSON数据查找
jsonpath是类似xpath的路径查找工具,可以方便地从JSON数据里查找到数据。
安装
pip install jsonpath
使用
测试数据
import jsonpath
import jsonjson_data = '''
{ "store": {"book": [ { "category": "reference","author": "Nigel Rees","title": "Sayings of the Century","price": 8.95},{ "category": "fiction","author": "Evelyn Waugh","title": "Sword of Honour","price": 12.99},{ "category": "fiction","author": "Herman Melville","title": "Moby Dick","isbn": "0-553-21311-3","price": 8.99},{ "category": "fiction","author": "J. R. R. Tolkien","title": "The Lord of the Rings","isbn": "0-395-19395-8","price": 22.99}],"bicycle": {"color": "red","price": 19.95}}
}
'''data = json.loads(json_data)
从根目录递归
# 从根目录递归匹配键bicycle
bicycle = jsonpath.jsonpath(data, '$..bicycle')
print(bicycle)
# [{'color': 'red', 'price': 19.95}]
$
表示根目录,..
表示遍历。
键过滤
# 从根目录递归匹配book,并且当前子键是isbn
isbn = jsonpath.jsonpath(data, '$..book[?(@.isbn)]')
print(isbn)
# [{'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}, {'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]
?()
用来过滤。
取值
author = jsonpath.jsonpath(data, '$..author')
print(author)
# ['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien']
author下面是键值对的值。
多选
#提取bicycle中color,price
color = jsonpath.jsonpath(data, '$..bicycle[color,price]]')
print(color)
# ['red', 19.95]
[,]
可以填写多个键,用来多选。
[]和.写法
# 跟$..bicycle.price一样
price = jsonpath.jsonpath(data, '$..bicycle[price]')
print(price)
#[19.95]
.
不能多选。
值过滤
# 取价格小于15的book
price = jsonpath.jsonpath(data, '$..book[?(@.price<15)]')
print(price)
# [{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, {'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99}, {'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}]
第几个
# 取第二个
book1 = jsonpath.jsonpath(data, '$..book[1]')
print(book1)
# [{'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99}]# 取前两个
book2 = jsonpath.jsonpath(data, '$..book[:2]')
print(book2)
# [{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, {'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99}]
[]
里的数字从0开始。
参考
https://goessner.net/articles/JsonPath/