当前位置: 首页 > news >正文

基于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/


http://www.mrgr.cn/news/35378.html

相关文章:

  • 【深度】边缘计算神器之数据网关
  • 如何选择游戏高防服务器,有什么需要注意的点?
  • 14.第二阶段x86游戏实战2-C++语言开发环境搭建-VisualStudio2017
  • 【工具】语音朗读PDF的免费工具
  • 《程序猿之设计模式实战 · 适配器模式》
  • 【软件测试】详解测试中常用的几种测试方法
  • 图文组合商标部分驳回后优化后初审通过!
  • C++类和对象(中)【下篇】
  • 使用chatglm3-6b来帮助我们构建菜谱数据集
  • 华为驱动未卸载导致内存完整性无法开启,导致lol卡顿,后台十几个重复进程
  • [万字长文]stable diffusion代码阅读笔记
  • 详解 C++中的模板
  • 响应式布局-媒体查询父级布局容器
  • 9.22日常记录
  • 3D Slicer医学图像全自动AI分割组合拳-MONAIAuto3DSeg扩展
  • 基于python的django微博内容网络分析系统,实现文本划分词结构
  • Spring Boot快速入门详解
  • 关于区块链的安全和隐私
  • 同等学力英语历年真题有必要做吗
  • [系统设计总结] - Proximity Service算法介绍