Python的jsonpath庫使用方法實(shí)例
一、簡介
接口返回的json數(shù)據(jù),需要取值后斷言,一般我們是使用jsonpath來提取接口返回的數(shù)據(jù)
JsonPath是一種信息抽取類庫,是從JSON文檔中抽取指定信息的工具。
JsonPath相當(dāng)于是Xpath 。
安裝
pip install jsonpath
二、語法說明
JsonPath解析的內(nèi)容必須是字典格式,通過JsonPath獲得的內(nèi)容,會以list的形式進(jìn)行返回 。
如果表達(dá)式出現(xiàn)錯誤,則會返回False(布爾類型的值)
jsonpath表達(dá)式的基本格式規(guī)范:
| 符號 | 描述 |
| $ | 表示根節(jié)點(diǎn),也是所有jsonpath表達(dá)式的開始 |
| . | 表示獲取子節(jié)點(diǎn) |
| .. | 表示獲取所有符合條件的內(nèi)容 |
| * | 代表所有的元素節(jié)點(diǎn) |
| [] | 表示迭代器的標(biāo)示(可以用于處理下標(biāo)等情況) |
| [,] | 表示多個結(jié)果的選擇 |
| ?() | 表示過濾操作 |
| @ | 表示當(dāng)前節(jié)點(diǎn) |
三、代碼示例
import jsonpath
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
}
}
}
# 獲取所有book的價格
price = jsonpath.jsonpath(data, '$.store.book[*].price')
print(price)
>> [8.95, 12.99, 8.99, 22.99]
# 獲取部分book節(jié)點(diǎn)下的price節(jié)點(diǎn)值,可支持切片操作
res1 = jsonpath.jsonpath(data, '$.store.book[0,1].price')
res2 = jsonpath.jsonpath(data, '$.store.book[0:2].price')
print(res1)
print(res2)
>> [8.95, 12.99]
>> [8.95, 12.99]
# 獲取最后一本書的價格(不能通過[-1]這種方式獲取)
res3 = jsonpath.jsonpath(data, '$.store.book[-1:].price')
print(res3)
>> [22.99]
# 獲取store節(jié)點(diǎn)下所有price節(jié)點(diǎn)的值
res4 = jsonpath.jsonpath(data, '$.store...price')
res5 = jsonpath.jsonpath(data, '$..price')
print(res4)
print(res5)
>> [8.95, 12.99, 8.99, 22.99, 19.95]
>> [8.95, 12.99, 8.99, 22.99, 19.95]
# jsonpath解析錯誤,返回bool類型數(shù)據(jù)false
res6 = jsonpath.jsonpath(data, '$.store.book111')
print(res6)
>> False
# 獲取價格大于10的所有書籍信息
book = jsonpath.jsonpath(data, '$..book[?(@.price>10)]')
print(book)
>> [{'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99}, {'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]
# 獲取book的價格大于10的prcice
price = jsonpath.jsonpath(data, '$..book[?(@.price>10)].price')
print(price)
>> [12.99, 22.99]
# 過濾所有帶有 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}]到此這篇關(guān)于Python的jsonpath庫使用方法實(shí)例的文章就介紹到這了,更多相關(guān)Python的jsonpath庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Tensorflow之梯度裁剪的實(shí)現(xiàn)示例
這篇文章主要介紹了Tensorflow之梯度裁剪的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Python如何通過百度翻譯API實(shí)現(xiàn)翻譯功能
這篇文章主要介紹了Python如何通過百度翻譯API實(shí)現(xiàn)翻譯功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
Python的線程使用隊(duì)列Queue來改造轉(zhuǎn)賬場景
前篇我們了隊(duì)列Queue和轉(zhuǎn)賬場景這次趁熱學(xué)委展示一下使用隊(duì)列解決轉(zhuǎn)賬場景的問題,這篇文章主要介紹了Python的線程使用隊(duì)列來改造轉(zhuǎn)賬場景,需要的朋友可以參考一下2022-02-02
Python基于DeepSeek大模型的提示詞優(yōu)化方案
以下基于DeepSeek大模型特性及搜索結(jié)果的綜合分析,結(jié)合提示詞設(shè)計原則、技術(shù)原理與優(yōu)化策略,提供完整Python代碼案例及詳細(xì)解析,需要的朋友可以參考下2025-04-04
Python中Tensorflow無法調(diào)用GPU問題的解決方法
文章詳解如何解決TensorFlow在Windows無法識別GPU的問題,需降級至2.10版本,安裝匹配CUDA?11.2和cuDNN?8.9.7,配置環(huán)境變量,并正確安裝TensorFlow?CPU版以確保GPU支持,需要的朋友可以參考下2025-06-06

