Python Json读写操作之JsonPath用法详解

作者:笃℃ 时间:2022-09-20 04:18:48 

Python Json读写操作_JsonPath用法详解

1. 介绍

JSONPath是一种信息抽取类库,是从JSON文档中抽取指定信息的工具,提供多种语言实现版本,包括Javascript、Python、PHP和Java。

JSONPath的安装方法如下:pip install jsonpath

JSONPath语法和XPATH语法对比,JSON结构清晰,可读性高,复杂度低,非常容易匹配。JSONPath的语法与Xpath类似,如下表所示为JSONPath与XPath语法对比:

Python Json读写操作之JsonPath用法详解

2. 代码示例

bookJson = {
 "store": {
   "book":[
     { "category": "reference",
       "author": "Nigel Rees",
       "title": "Sayings of the Century",
       "price": 8.95
     },
     { "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
   }
 }
}

变量bookJson中已经包含了这段JSON字符串,可通过以下代码反序列化得到JSON对象:

books=json.loads(bookJson)

1)查看store下的bicycle的color属性:

checkurl = "$.store.bicycel.color"
print(jsonpath.jsonpath(books, checkurl))
# 输出:['red']

2)输出book节点中包含的所有对象:

checkurl = "$.store.book[*]"
object_list=jsonpath.jsonpath(books, checkurl)
print(object_list)

3)输出book节点的第一个对象:

checkurl = "$.store.book[0]"
obj = jsonpath.jsonpath(books, checkurl)
print(obj)
# 输出: ['category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}]

4)输出book节点中所有对象对应的属性title值:

checkurl = "$.store.book[*].title"
titles = jsonpath.jsonpath(books, checkurl)
print(titles)
# 输出: ['Sayings of the Century', 'The Lord of the Rings']

5)输出book节点中category为fiction的所有对象:

checkurl = "$.store.book[?(@.category=='fiction')]”
books=jsonpath.jsonpath(books, checkurl)
print(books)
# 输出:[{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lordof the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]

6)输出book节点中所有价格小于10的对象:

checkurl="$.store.book[?(@.price<10)]"
books = jsonpath.jsonpath(books, checkurl)
print(books)
# 输出: [{'category': 'reference', 'author': 'Nigel Rees', 'title':'Sayings of the Century', 'price': 8.95}]

7)输出book节点中所有含有isb的对象:

checkurl = "$.store.book[?(@.isb)]"
books = jsonpath.jsonpath(books,checkurl)
print(books)
# 输出: [{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]

3. 参考

【1】https://blog.csdn.net/fallenjency/article/details/123276600

来源:https://blog.csdn.net/qq_51392112/article/details/130132976

标签:Python,JsonPath
0
投稿

猜你喜欢

  • Python 如何创建一个简单的REST接口

    2022-03-07 17:03:16
  • Python 必须了解的5种高级特征

    2023-09-27 23:00:16
  • python办公之python编辑word

    2022-03-31 08:48:40
  • python删除某个目录文件夹的方法

    2022-08-22 06:33:32
  • XML教程 WEB页面工具语言XML的定义

    2008-05-29 10:54:00
  • OpenCV-Python实现怀旧滤镜与连环画滤镜

    2023-01-29 08:56:22
  • mac 上配置Pycharm连接远程服务器并实现使用远程服务器Python解释器的方法

    2021-10-19 18:40:40
  • Git远程操作详解

    2022-02-11 06:15:51
  • Pygame的程序开始示例代码

    2021-12-20 19:45:01
  • python计算Content-MD5并获取文件的Content-MD5值方式

    2022-09-27 00:53:29
  • JS表单验证插件之数据与逻辑分离操作实例分析【策略模式】

    2024-04-25 13:14:08
  • vue+canvas实现数据实时从上到下刷新瀑布图效果(类似QT的)

    2024-05-09 09:16:28
  • Python的collections模块中的OrderedDict有序字典

    2023-06-21 00:13:52
  • Python模块学习 filecmp 文件比较

    2023-07-09 18:30:44
  • wxPython定时器wx.Timer简单应用实例

    2022-08-20 00:36:30
  • 最常用的SQL语句

    2024-01-23 20:37:40
  • ubuntu下在docker中安装mysql5.6 的方法

    2024-01-23 08:35:30
  • 再次探讨go实现无限 buffer 的 channel方法

    2024-02-10 23:34:54
  • JS获取当前时间的年月日时分秒及时间的格式化的方法

    2024-04-17 10:23:00
  • MSSQL2005数据库备份导入MSSQL2000

    2024-01-22 12:59:56
  • asp之家 网络编程 m.aspxhome.com