python中jsonpath的使用小結(jié)
介紹
JSONPath能在復(fù)雜的JSON數(shù)據(jù)中 查找和提取所需的信息,它是一種功能強(qiáng)大的查詢語(yǔ)言,可以通過(guò)簡(jiǎn)單的表達(dá)式來(lái)快速準(zhǔn)確地定位和提取JSON數(shù)據(jù)。本文將介紹JSONPath的基本語(yǔ)法和用法,并為您展示如何封裝和使用JSONPath方法來(lái)處理和操作JSON數(shù)據(jù)。
JSONPath類似于XPath提供了一種更簡(jiǎn)潔、靈活和高效的方式來(lái)查詢、定位和提取JSON數(shù)據(jù)中的內(nèi)容
安裝
pip install jsonpath
語(yǔ)法
from jsonpath import jsonpath res = jsonpath(dict,'jsonpath語(yǔ)法規(guī)則字符串')
語(yǔ)法規(guī)則
| JsonPath | 描述 |
|---|---|
| $ | 根節(jié)點(diǎn)元素,最外層的大括號(hào) |
| @ | 當(dāng)前節(jié)點(diǎn)元素 |
| . 或 [] | 絕對(duì)路徑,取子節(jié)點(diǎn)元素 |
| … | 相對(duì)路徑,內(nèi)部的任意位置,選擇符合條件的子孫節(jié)點(diǎn)元素 |
| * | 匹配所有元素節(jié)點(diǎn) |
| [] | 迭代器提示(可以在里邊做簡(jiǎn)單的迭代操作,如數(shù)組下標(biāo),根據(jù)內(nèi)容篩選) |
| [,] | 支持迭代器中做多選 |
| ?() | 支持過(guò)濾操作 |
| () | 支持表達(dá)式計(jì)算 |
舉例說(shuō)明
獲取書(shū)架上的書(shū)進(jìn)行操作
- 數(shù)據(jù)結(jié)構(gòu):
book_dict = {
"store":{
"book":[
{
"name": "java 核心編程1",
"price": "65",
"isbn": "IS002598934",
"author" : "周立新"
},
{
"name": "java 核心編程2",
"price": "64",
"isbn": "IS876430456",
"author": "周立新"
},
{
"name" : "java 編程思想",
"price": "120",
"isbn": "IS256709873",
"author": "Bruce Eckel"
},
{
"name" : "RabbitMq 實(shí)戰(zhàn)指南",
"price": "79",
"isbn": "IS987623450",
"author": "朱忠華"
},
{
"name" : "圖解 TCP/IP",
"price": "69",
"isbn": "IS9787354679",
"author": "竹下隆史"
}
]
}
}
- 結(jié)構(gòu)解析例子
| JsonPath | 結(jié)果 |
|---|---|
| $.store.book[*].author | store 中的所有的 book的作者 |
| $…store.* | 所有的書(shū)籍 |
| $.store…price | store 中的所有的內(nèi)容的價(jià)格 |
| $…book[2] | 第三本書(shū) |
| $…book[(@.length-1)] 或 $…book[-1:] | 最后一本書(shū) |
| $…book[0,1] 或 $…book[:2] | 前兩本書(shū) |
| $…book[?(@.isbn)] | 獲取有 isbn 的所有數(shù) |
| $…book[?(@.price<100)] | 獲取價(jià)格<100的所有的書(shū) |
| $…* | 獲取所有的數(shù)據(jù) |
在 python 中使用
如下代碼都用單元測(cè)試來(lái)舉例,具體代碼鏈接:去這里
- 設(shè)置測(cè)試結(jié)構(gòu)
def setUp(self):
"""
前置設(shè)置
@return:
"""
self.book_dict = {
"store": {
"book": [
{
"name": "java 核心編程1",
"price": 65,
"isbn": "IS002598934",
"author": "周立新"
},
{
"name": "java 核心編程2",
"price": 64,
"isbn": "IS876430456",
"author": "周立新"
},
{
"name": "java 編程思想",
"price": 120,
"isbn": "IS256709873",
"author": "Bruce Eckel"
},
{
"name": "RabbitMq 實(shí)戰(zhàn)指南",
"price": 79,
"isbn": "IS987623450",
"author": "朱忠華"
},
{
"name": "圖解 TCP/IP",
"price": 69,
"isbn": "IS9787354679",
"author": "竹下隆史"
}
]
}
}
pass
獲取所有結(jié)構(gòu)
def test_all(self):
"""
獲取所有的結(jié)構(gòu)
@return:
"""
dict_data = jsonpath(self.book_dict, '$..*')
print("查看dict_data支持的屬性和方法:", dir(dict_data))
try:
for item in dict_data[0]['book']:
# 美化打印
pprint(item)
except Exception as e:
print(e)
pass
所有子節(jié)點(diǎn)的作者
def test_sub_author(self):
"""
獲取所有子節(jié)點(diǎn)的作者
@return:
"""
all_author = jsonpath(self.book_dict, '$.store.book[*].author')
print(all_author)
pass
獲取所有子孫節(jié)點(diǎn)
def test_sub_all(self):
"""
獲取所有子孫節(jié)點(diǎn)
@return:
"""
sub_all = jsonpath(self.book_dict, '$..store.*')
print(sub_all)
pass
獲取所有價(jià)格
def test_price_all(self):
"""
獲取子節(jié)點(diǎn)的所有價(jià)格
@return:
"""
price_all = jsonpath(self.book_dict, '$..price')
print("所有的價(jià)格:", price_all)
price_sum = sum([int(price) for price in price_all])
print(f"價(jià)格之和:{price_sum}")
pass
取出第三本書(shū)的所有信息
def test_book_item(self):
"""
取出第三本書(shū)的所有信息
@return:
"""
book_item = jsonpath(self.book_dict, '$..book[2]')
print(book_item)
pass
取出價(jià)格大于70塊的所有書(shū)本
def test_book_filter(self):
book_count = jsonpath(self.book_dict, '$..book[?(@.price>70)]')
print(book_count)
從mongodb 中取數(shù)據(jù)的示例
遇到復(fù)雜的結(jié)構(gòu)可以使用 pprint()來(lái)美化打印
def test_mongo_data(self):
"""
獲取mongo中的復(fù)雜結(jié)構(gòu)的數(shù)據(jù)
@return:
"""
dict_data = MongoPool().project_8.coffer_check_data.find_one({"_id": "629dbfe615e74466831b5ce2"})
# 美化打印
pprint(dict_data)
change_list = jsonpath(dict_data, '$..change')
print("獲取某一個(gè)字字段的列表:", change_list)
pass
效果:

到此這篇關(guān)于python中jsonpath的使用小結(jié)的文章就介紹到這了,更多相關(guān)python jsonpath使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)在PowerPoint演示文稿中添加漏斗圖
漏斗圖是一種常用的數(shù)據(jù)可視化工具,本文將介紹如何使用?Python?和?Spire.Presentation?for?Python?庫(kù)在?PowerPoint?演示文稿中創(chuàng)建專業(yè)的漏斗圖,文中的示例代碼講解詳細(xì),有需要的小伙伴可以了解下2026-06-06
python flask解析json數(shù)據(jù)不完整的解決方法
這篇文章主要介紹了python flask解析json數(shù)據(jù)不完整的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05
Python實(shí)現(xiàn)批量讀取word中表格信息的方法
這篇文章主要介紹了Python實(shí)現(xiàn)批量讀取word中表格信息的方法,可實(shí)現(xiàn)針對(duì)word文檔的讀取功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
Python利用PyMobileDevice3控制iOS設(shè)備的完整教程
PyMobileDevice3是一個(gè)純Python 3實(shí)現(xiàn)的iOS設(shè)備控制工具庫(kù),為開(kāi)發(fā)者和技術(shù)愛(ài)好者提供了強(qiáng)大的iOS設(shè)備管理能力,所以本文給大家介紹了Python利用PyMobileDevice3控制iOS設(shè)備的完整教程,需要的朋友可以參考下2025-12-12
數(shù)據(jù)驅(qū)動(dòng)測(cè)試DDT之Selenium讀取Excel文件
這篇文章主要為大家介紹了數(shù)據(jù)驅(qū)動(dòng)測(cè)試DDT之Selenium讀取Excel文件,2021-11-11
python向企業(yè)微信發(fā)送文字和圖片消息的示例
這篇文章主要介紹了python向企業(yè)微信發(fā)送文字和圖片消息的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-09-09

