python中jsonpath的使用小結(jié)
介紹
JSONPath能在復雜的JSON數(shù)據(jù)中 查找和提取所需的信息,它是一種功能強大的查詢語言,可以通過簡單的表達式來快速準確地定位和提取JSON數(shù)據(jù)。本文將介紹JSONPath的基本語法和用法,并為您展示如何封裝和使用JSONPath方法來處理和操作JSON數(shù)據(jù)。
JSONPath類似于XPath提供了一種更簡潔、靈活和高效的方式來查詢、定位和提取JSON數(shù)據(jù)中的內(nèi)容
安裝
pip install jsonpath
語法
from jsonpath import jsonpath res = jsonpath(dict,'jsonpath語法規(guī)則字符串')
語法規(guī)則
JsonPath | 描述 |
---|---|
$ | 根節(jié)點元素,最外層的大括號 |
@ | 當前節(jié)點元素 |
. 或 [] | 絕對路徑,取子節(jié)點元素 |
… | 相對路徑,內(nèi)部的任意位置,選擇符合條件的子孫節(jié)點元素 |
* | 匹配所有元素節(jié)點 |
[] | 迭代器提示(可以在里邊做簡單的迭代操作,如數(shù)組下標,根據(jù)內(nèi)容篩選) |
[,] | 支持迭代器中做多選 |
?() | 支持過濾操作 |
() | 支持表達式計算 |
舉例說明
獲取書架上的書進行操作
- 數(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 實戰(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.* | 所有的書籍 |
$.store…price | store 中的所有的內(nèi)容的價格 |
$…book[2] | 第三本書 |
$…book[(@.length-1)] 或 $…book[-1:] | 最后一本書 |
$…book[0,1] 或 $…book[:2] | 前兩本書 |
$…book[?(@.isbn)] | 獲取有 isbn 的所有數(shù) |
$…book[?(@.price<100)] | 獲取價格<100的所有的書 |
$…* | 獲取所有的數(shù)據(jù) |
在 python 中使用
如下代碼都用單元測試來舉例,具體代碼鏈接:去這里
- 設置測試結(jié)構(gòu)
def setUp(self): """ 前置設置 @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 實戰(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é)點的作者
def test_sub_author(self): """ 獲取所有子節(jié)點的作者 @return: """ all_author = jsonpath(self.book_dict, '$.store.book[*].author') print(all_author) pass
獲取所有子孫節(jié)點
def test_sub_all(self): """ 獲取所有子孫節(jié)點 @return: """ sub_all = jsonpath(self.book_dict, '$..store.*') print(sub_all) pass
獲取所有價格
def test_price_all(self): """ 獲取子節(jié)點的所有價格 @return: """ price_all = jsonpath(self.book_dict, '$..price') print("所有的價格:", price_all) price_sum = sum([int(price) for price in price_all]) print(f"價格之和:{price_sum}") pass
取出第三本書的所有信息
def test_book_item(self): """ 取出第三本書的所有信息 @return: """ book_item = jsonpath(self.book_dict, '$..book[2]') print(book_item) pass
取出價格大于70塊的所有書本
def test_book_filter(self): book_count = jsonpath(self.book_dict, '$..book[?(@.price>70)]') print(book_count)
從mongodb 中取數(shù)據(jù)的示例
遇到復雜的結(jié)構(gòu)可以使用 pprint()來美化打印
def test_mongo_data(self): """ 獲取mongo中的復雜結(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("獲取某一個字字段的列表:", change_list) pass
效果:
到此這篇關于python中jsonpath的使用小結(jié)的文章就介紹到這了,更多相關python jsonpath使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python實現(xiàn)的對本地host127.0.0.1主機進行掃描端口功能示例
這篇文章主要介紹了Python實現(xiàn)的對本地host127.0.0.1主機進行掃描端口功能,可實現(xiàn)掃描本機開放端口的功能,涉及Python socket模塊與Thread多線程模塊相關使用技巧,需要的朋友可以參考下2019-02-02PySide(PyQt)使用QPropertyAnimation制作動態(tài)界面的示例代碼
文章介紹了如何使用PySide或PyQt的QPropertyAnimation類來創(chuàng)建動態(tài)界面效果,感興趣的朋友一起看看吧2025-03-03如何利用pandas工具輸出每行的索引值、及其對應的行數(shù)據(jù)
這篇文章主要介紹了如何利用pandas工具輸出每行的索引值、及其對應的行數(shù)據(jù),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03python調(diào)用系統(tǒng)ffmpeg實現(xiàn)視頻截圖、http發(fā)送
這篇文章主要為大家詳細介紹了python調(diào)用系統(tǒng)ffmpeg實現(xiàn)視頻截圖、http發(fā)送,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03python深度學習標準庫使用argparse調(diào)參
這篇文章主要為大家介紹了python深度學習標準庫使用argparse調(diào)參實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06詳解Python3 中hasattr()、getattr()、setattr()、delattr()函數(shù)及示例代碼數(shù)
本文通過示例代碼給大家詳細介紹了Python3 中hasattr()、getattr()、setattr()、delattr()函數(shù),非常不錯,具有參考借鑒價值,需要的朋友參考下吧2018-04-04