python中jsonpath的使用小結(jié)
介紹
JSONPath能在復(fù)雜的JSON數(shù)據(jù)中 查找和提取所需的信息,它是一種功能強(qiáng)大的查詢(xún)語(yǔ)言,可以通過(guò)簡(jiǎn)單的表達(dá)式來(lái)快速準(zhǔn)確地定位和提取JSON數(shù)據(jù)。本文將介紹JSONPath的基本語(yǔ)法和用法,并為您展示如何封裝和使用JSONPath方法來(lái)處理和操作JSON數(shù)據(jù)。
JSONPath類(lèi)似于XPath提供了一種更簡(jiǎn)潔、靈活和高效的方式來(lái)查詢(xún)、定位和提取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)的對(duì)本地host127.0.0.1主機(jī)進(jìn)行掃描端口功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)的對(duì)本地host127.0.0.1主機(jī)進(jìn)行掃描端口功能,可實(shí)現(xiàn)掃描本機(jī)開(kāi)放端口的功能,涉及Python socket模塊與Thread多線程模塊相關(guān)使用技巧,需要的朋友可以參考下2019-02-02PySide(PyQt)使用QPropertyAnimation制作動(dòng)態(tài)界面的示例代碼
文章介紹了如何使用PySide或PyQt的QPropertyAnimation類(lèi)來(lái)創(chuàng)建動(dòng)態(tài)界面效果,感興趣的朋友一起看看吧2025-03-03Python3.x爬蟲(chóng)下載網(wǎng)頁(yè)圖片的實(shí)例講解
今天小編就為大家分享一篇Python3.x爬蟲(chóng)下載網(wǎng)頁(yè)圖片的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05如何利用pandas工具輸出每行的索引值、及其對(duì)應(yīng)的行數(shù)據(jù)
這篇文章主要介紹了如何利用pandas工具輸出每行的索引值、及其對(duì)應(yīng)的行數(shù)據(jù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03python調(diào)用系統(tǒng)ffmpeg實(shí)現(xiàn)視頻截圖、http發(fā)送
這篇文章主要為大家詳細(xì)介紹了python調(diào)用系統(tǒng)ffmpeg實(shí)現(xiàn)視頻截圖、http發(fā)送,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03python深度學(xué)習(xí)標(biāo)準(zhǔn)庫(kù)使用argparse調(diào)參
這篇文章主要為大家介紹了python深度學(xué)習(xí)標(biāo)準(zhǔn)庫(kù)使用argparse調(diào)參實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06詳解Python3 中hasattr()、getattr()、setattr()、delattr()函數(shù)及示例代碼數(shù)
本文通過(guò)示例代碼給大家詳細(xì)介紹了Python3 中hasattr()、getattr()、setattr()、delattr()函數(shù),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-04-04