在介面測試或其他測試場景中,我們經常需要獲取JSON中的某路徑下的值進行校驗,即從JSON中抽取指定信息。
本文主要介紹滿足該需求的Python jmespath庫,除此之外還有Jsonpath,有興趣的可以自行了解。
安裝
pip install jmespath代碼示例
我們以下面test_json 這個較為複雜的Json對象為示例。
test_json = {
"code": 200,
"desc": {
"info":"說明",
"update":"2021年3月6日"
},
"data": [
{
"apps": {
"app_op_seq": [
{
"action": "點擊",
"module_name": "聚划算",
"module_type": "resource"
}
]
},
"content": {
"des": {
"company_name": "耐克",
"intent": [
"full"
]
},
"rel": [
{
"des": {
"person_name": "歐陽玖林",
"political_status": "金牌會員"
},
"ont": [
{
"name":"Person",
"id":1
},
{
"name":"Company",
"id":2
},
{
"name":"Car",
"id":3
}
],
"relIdx": [
0,
"8-9"
],
"relName": "歐陽",
"segs": [
"耐克籃球鞋"
]
}
],
"segs": [
"耐克籃球鞋"
]
},
"content_op": "查詢"
}
]
}
我們先用之前文章中介紹的自研 《Python 實現 JSON、字典數據結構的遞歸解析》 代碼,解析上述 JSON對象,解析結果如下:
1 JsonPath:code Value:200
2 JsonPath:desc.info Value:說明
3 JsonPath:desc.update Value:2021年3月6日
4 JsonPath:data[0].apps.app_op_seq[0].action Value:點擊
5 JsonPath:data[0].apps.app_op_seq[0].module_name Value:聚划算
6 JsonPath:data[0].apps.app_op_seq[0].module_type Value:resource
7 JsonPath:data[0].content.des.company_name Value:耐克
8 JsonPath:data[0].content.des.intent[0] Value:full
9 JsonPath:data[0].content.rel[0].des.person_name Value:歐陽玖林
10 JsonPath:data[0].content.rel[0].des.political_status Value:金牌會員
11 JsonPath:data[0].content.rel[0].ont[0].name Value:Person
12 JsonPath:data[0].content.rel[0].ont[0].id Value:1
13 JsonPath:data[0].content.rel[0].ont[1].name Value:Company
14 JsonPath:data[0].content.rel[0].ont[1].id Value:2
15 JsonPath:data[0].content.rel[0].ont[2].name Value:Car
16 JsonPath:data[0].content.rel[0].ont[2].id Value:3
17 JsonPath:data[0].content.rel[0].relIdx[0] Value:0
18 JsonPath:data[0].content.rel[0].relIdx[1] Value:8-9
19 JsonPath:data[0].content.rel[0].relName Value:歐陽
20 JsonPath:data[0].content.rel[0].segs[0] Value:耐克籃球鞋
21 JsonPath:data[0].content.segs[0] Value:耐克籃球鞋
22 JsonPath:data[0].content_op Value:查詢
Process finished with exit code 0
Demo1 查詢某個key的值
import jmespath
result = jmespath.search("code",test_json)
print(result)
# 執行上述代碼,輸出結果如下:
200Demo2 層級查詢某個key的值
import jmespath
result = jmespath.search("desc.info",test_json)
print(result)
# 執行上述代碼,輸出結果如下:
說明Demo3 通過索引查詢Json中List 元素
import jmespath
result = jmespath.search("data[0]",test_json)
print(result)
# 執行上述代碼,輸出結果如下:
{'apps': {'app_op_seq': [{'action': '點擊', 'module_name': '聚划算', 'module_type': 'resource'}]}, 'content': {'des': {'company_name': '耐克', 'intent': ['full']}, 'rel': [{'des': {'person_name': '歐陽玖林', 'political_status': '金牌會員'}, 'ont': [{'name': 'Person', 'id': 1}, {'name': 'Company', 'id': 2}, {'name': 'Car', 'id': 3}], 'relIdx': [0, '8-9'], 'relName': '歐陽', 'segs': ['耐克籃球鞋']}], 'segs': ['耐克籃球鞋']}, 'content_op': '查詢'}
Demo4 複雜層級查詢某個key的值
import jmespath
result = jmespath.search("data[0].apps.app_op_seq[0].action",test_json)
print(result)
# 執行上述代碼,輸出結果如下:
點擊通過上述查詢方式,我們基本也能看出,在取列表值時用的 “[]”,取字典值時用的 “.”,來表示路徑層級 。
Demo5 對Json中List進行切片查詢
import jmespath
# 獲取relIdx下第0、1個元素
result = jmespath.search("data[0].content.rel[0].relIdx[0:2]",test_json)
print(result)
# 獲取relIdx下全部元素 relIdx[*]
result1 = jmespath.search("data[0].content.rel[0].relIdx[*]",test_json)
print(result1)
# 執行上述代碼,輸出結果如下:
[0, '8-9']
[0, '8-9']Demo6 其他用法
import jmespath
# 批量獲取Json中List的字典元素的某個Key的值
result = jmespath.search("data[0].content.rel[0].ont[0:3].name",test_json)
print(result)
# 批量獲取Json中List的字典元素的多個Key的值
result1 = jmespath.search("data[0].content.rel[0].ont[0:3].[name,id]",test_json)
print(result2)
# 執行上述代碼,輸出結果如下:
['Person', 'Company', 'Car']
[['Person', 1], ['Company', 2], ['Car', 3]]
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/222995.html
微信掃一掃
支付寶掃一掃