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

基于jsonpath的JSON数据查找

jsonpath是类似xpath的路径查找工具,可以方便地从JSON数据里查找到数据。

安装

pip install jsonpath

使用

测试数据

import jsonpath
import json

json_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.kler.cn/news/318337.html

相关文章:

  • golang学习笔记1-go程序执行流程
  • element-plus表格操作
  • C++之哈希 --- 哈希的应用(位图布隆过滤器)
  • 【鸿蒙】HarmonyOS NEXT开发快速入门教程之ArkTS语法装饰器(上)
  • 【软件测试】详解测试中常用的几种测试方法
  • 从更底层的角度理解网站的访问过程
  • 算法打卡:第十一章 图论part05
  • 关于Python升级以后脚本不能运行的问题
  • MongoDB-aggregate流式计算:去重操作
  • Linux下go环境安装、环境配置并执行第一个go程序
  • python多继承 - 子类指定父类
  • 【教程】鸿蒙ARKTS 打造数据驾驶舱---前序
  • 两数之和、三数之和、四数之和
  • 在mac中如何使python3作为默认版本
  • 用canvas画一个验证码
  • 从 Oracle 集群到单节点环境(详细记录一次数据迁移过程)之一:生产环境与目标服务器详情
  • 基于物联网的火灾报警器设计与实现(论文+源码)
  • 高维数据和超高维数据
  • CX8903:电动车手机充电器降压芯片,搭配协议实现快充
  • Linux入门学习:进程概念
  • k8s前置准备:配置虚拟机网络
  • 计算机网络 --- 初识协议
  • 多人在线聊天服务器
  • P9235 [蓝桥杯 2023 省 A] 网络稳定性
  • Unity教程(十六)敌人攻击状态的实现
  • 【WebLogic】WebLogic 11g 控制台模式下的集群创建(一)
  • JetBrains系列产品无限重置免费试用方法
  • ATTCK实战系列-Vulnstack靶场内网域渗透(二)
  • Spring-bean的生命周期-中篇
  • 光伏开发:一分钟生成光伏项目报告