Python中jmespath解析提取json数据
? ? ? ? 在做接口自動化,測試斷言時,我們經常需要提取接口的的響應數據字段,以前用過jsonpath,有幾篇相關文章,可以參考下(Python深層解析json數據之JsonPath、【Jmeter篇】后置處理器之正則提取器、Json提取器?、Jmeter之json提取器實戰(二)、Jmeter之json條件提取實戰(三)?)今天我們來介紹下jmespath用法,可以幫我們進行數據的靈活提取,下面通過案例來說明jmespath在python的使用。
jmespath官方文檔
https://jmespath.org/tutorial.html#projections
jmespath安裝
pip install jmespath
字典,通過key名稱提取
嵌套字典,層級提取
import jmespath dict_1 = {"a": {"b": {"c": {"d": "value"}}}} print(jmespath.search("a.b.c.d",dict_1))valueimport jmespath dict_1 = {"a": {"b": {"c": {"d": "value"}}}} print(jmespath.search("a.b.c",dict_1)){'d': 'value'}列表,通過索引提取
列表、字典嵌套提取
切片提取列表中值
列表取值使用 * 通配符
1、取出列表中所有?first對應的值
?people[*].first
import jmespath source = {"people": [{"first": "James", "last": "d"},{"first": "Jacob", "last": "e"},{"first": "Jayden", "last": "f"},{"missing": "different"}],"foo": {"bar": "baz"} } print(jmespath.search("people[*].first",source))['James', 'Jacob', 'Jayden']2、取出列表中前2個first 對應的值
people[:2].first
import jmespath source = {"people": [{"first": "James", "last": "d"},{"first": "Jacob", "last": "e"},{"first": "Jayden", "last": "f"},{"missing": "different"}],"foo": {"bar": "baz"} } print(jmespath.search("people[:2].first",source))['James', 'Jacob']對象取值使用 * 通配符
取出ops 對象的任意屬性對應的numArgs值 ops.*.numArgs
import jmespath source = {"ops": {"functionA": {"numArgs": 2},"functionB": {"numArgs": 3},"functionC": {"variadic": 1}} } print(jmespath.search("ops.*.numArgs",source))[2, 3]子查詢使用 * 通配符
在查詢的結果中繼續使用 * 通配符,查詢的結果是列表的列表
import jmespath source = {"reservations": [{"instances": [{"state": "running"},{"state": "stopped"}]},{"instances": [{"state": "terminated"},{"state": "running"}]}] } print(jmespath.search("reservations[*].instances[*].state",source))[['running', 'stopped'], ['terminated', 'running']]我們希望結果為[“ running”, “ stopped”, “ terminated”, “ running”]一個狀態列表,可以使用 [] 而不是 [*]
print(jmespath.search("reservations[].instances[].state",source))['running', 'stopped', 'terminated', 'running']過濾器使用
過濾器表達式是為數組定義的,其一般形式為 [?<表達式> <比較器> <表達式>]。
常用的比較表達式可以使用 ==, !=, <, <=, >, > =
假設我們有一個設備列表,每個設備都有一個名稱和一個 state。我們想要所有正在運行的計算機的名稱
import jmespath source = {"machines": [{"name": "a", "state": "running"},{"name": "b", "state": "stopped"},{"name": "b", "state": "running"}] } print(jmespath.search("machines[?state=='running'].name",source))['a', 'b']管道表達式
前面在匹配list里面的多個值時候,查詢的結果是一個list,如果我想取出結果里面的第一個可以使用管道符 |
取出people下所有對象的 first 屬性,從結果里面取第一個值:people[*].first | [0]
import jmespath source = {"people": [{"first": "James", "last": "d"},{"first": "Jacob", "last": "e"},{"first": "Jayden", "last": "f"},{"missing": "different"}],"foo": {"bar": "baz"} } print(jmespath.search("people[*].first | [0]",source))James多選列表創建一個列表
到目前為止,我們已經研究了JMESPath表達式,這些表達式有助于將JSON文檔縮減為您感興趣的元素。下一個概念, 多選列表和 多選哈希允許您創建JSON元素。這使您可以創建JSON文檔中不存在的元素。多選列表創建一個列表,多選哈希創建一個JSON對象。
這是一個多選列表的示例:people[].[name, state.name]
import jmespath source = {"people": [{"name": "a","state": {"name": "up"}},{"name": "b","state": {"name": "down"}},{"name": "c","state": {"name": "up"}}] } print(jmespath.search("people[].[name, state.name]",source))[['a', 'up'], ['b', 'down'], ['c', 'up']]在上面的表達式中,[name, state.name]部分是一個多選列表。它說要創建一個由兩個元素組成的列表,第一個元素是針對list元素評估名稱表達式的結果,第二個元素是對state.name評估的結果。因此,每個列表元素將創建一個兩個元素列表,并且整個表達式的最終結果是兩個元素列表的列表。
與投影不同,即使結果為null,也始終包含表達式的結果。如果將以上表達式更改為people []。[foo, bar],則每個兩個元素列表將為[null, null]。
多重選擇具有與多重選擇列表相同的基本概念,不同之處在于它會創建哈希而不是數組。使用上面的相同示例,如果我們想創建一個具有兩個鍵Name和 State的兩個元素哈希,則可以使用以下代碼:
import jmespath source = {"people": [{"name": "a","state": {"name": "up"}},{"name": "b","state": {"name": "down"}},{"name": "c","state": {"name": "up"}}] } print(jmespath.search("people[].{Name: name, State: state.name}",source))[{'Name': 'a', 'State': 'up'}, {'Name': 'b', 'State': 'down'}, {'Name': 'c', 'State': 'up'}]函數的使用
JMESPath支持函數表達式,例如:length(people)
import jmespath source = {"people": [{"name": "b","age": 30,"state": {"name": "up"}},{"name": "a","age": 50,"state": {"name": "down"}},{"name": "c","age": 40,"state": {"name": "up"}}] } print(jmespath.search("length(people)",source))3函數可用于以強大的方式轉換和過濾數據??梢栽诖颂幷业胶瘮档耐暾斜?#xff0c;并且 函數表達式規范具有完整的詳細信息。
以下是一些功能示例。
本示例在people數組中打印最老的人的名字:
import jmespath source = {"people": [{"name": "b","age": 30},{"name": "a","age": 50},{"name": "c","age": 40}] } print(jmespath.search("max_by(people, &age).name",source))a函數也可以與過濾器表達式組合。在下面的示例中,JMESPath表達式在myarray中查找包含字符串foo的所有元素。
import jmespath source = {"myarray": ["foo","foobar","barfoo","bar","baz","barbaz","barfoobaz"] } print(jmespath.search("myarray[?contains(@, 'foo') == `true`]",source))['foo', 'foobar', 'barfoo', 'barfoobaz']場景一,接口響應數據,提取code、msg、status字段進行斷言
source = {"code": 0,"msg": "成功","trace": "ad12de4","data": {"total": 205,"list": [{"id": 15000087,"name": "促銷員","job_nature": 2,"category_id": 61,"user_id": 589601,"company_id": 5084,"group_id": 5084,"status": 4,"audit_type": 0,"company_name": "二十二門店","job_nature_zh": "兼職","salary_zh": "1000元\/時","show_status": 5,"manage_status_reason": "","status_zh": "停招","browse_users_num": 0,"communication_users_num": 0,"sign_up_users_num": 0,"job_card_time_remaining": 22,"job_top_card_time_remaining": 0}, {"id": 15000078,"name": "促銷員","job_nature": 1,"category_id": 61,"user_id": 589601,"company_id": 1000064,"group_id": 5084,"status": 4,"audit_type": 100,"company_name": "二十二門店","job_nature_zh": "全職","salary_zh": "2000-3000元\/月","show_status": 6,"manage_status_reason": "","status_zh": "停招","browse_users_num": 0,"communication_users_num": 0,"sign_up_users_num": 0,"job_card_time_remaining": 0,"job_top_card_time_remaining": 0}, {"id": 15000077,"name": "促銷員","job_nature": 1,"category_id": 61,"user_id": 589601,"company_id": 5084,"group_id": 5084,"status": 4,"audit_type": 100,"company_name": "二十二門店","job_nature_zh": "全職","salary_zh": "2000-3000元\/月","show_status": 2,"manage_status_reason": "","status_zh": "停招","browse_users_num": 0,"communication_users_num": 0,"sign_up_users_num": 0,"job_card_time_remaining": 0,"job_top_card_time_remaining": 0}, {"id": 13076362,"name": "收銀員","job_nature": 2,"category_id": 97,"user_id": 589601,"company_id": 5084,"group_id": 5084,"status": 4,"audit_type": 1,"company_name": "二十二門店","job_nature_zh": "兼職","salary_zh": "1000元\/時","show_status": 2,"manage_status_reason": "","status_zh": "停招","browse_users_num": 0,"communication_users_num": 0,"sign_up_users_num": 0,"job_card_time_remaining": 0,"job_top_card_time_remaining": 0}, {"id": 13076361,"name": "品類管理","job_nature": 1,"category_id": 102,"user_id": 589601,"company_id": 5084,"group_id": 5084,"status": 1,"audit_type": 1,"company_name": "二十二門店","job_nature_zh": "全職","salary_zh": "2000-3000元\/月","show_status": 1,"manage_status_reason": "","status_zh": "招聘中","browse_users_num": 3,"communication_users_num": 1,"sign_up_users_num": 1,"job_card_time_remaining": 342,"job_top_card_time_remaining": 0}]} }print(jmespath.search("code",source)) # 0 print(jmespath.search("msg",source)) # 成功 print(jmespath.search("data.list[].status",source)) # [4, 4, 4, 4, 1]場景二,接口響應數據,提取列表類模塊某字段進行斷言
import jmespath source = [{"Name": "晨練指數","ID": 100,"Ascending": 1,"LocalDateTime": "2021-11-11T07:00:00+08:00","EpochDateTime": 1636585200,"Value": 4.0,"Category": "不宜","CategoryValue": 4,"MobileLink": "http://m.weathercn.com/zh/cn/pudong-new-district/74761/weather-forecast/74761?lang=zh-cn","Link": "http://m.weathercn.com/zh/cn/pudong-new-district/74761/weather-forecast/74761?lang=zh-cn" }, {"Name": "穿衣指數","ID": 101,"Ascending": 1,"LocalDateTime": "2021-11-11T07:00:00+08:00","EpochDateTime": 1636585200,"Value": 5.0,"Category": "初冬裝","CategoryValue": 5,"MobileLink": "http://m.weathercn.com/zh/cn/pudong-new-district/74761/weather-forecast/74761?lang=zh-cn","Link": "http://m.weathercn.com/zh/cn/pudong-new-district/74761/weather-forecast/74761?lang=zh-cn" }, {"Name": "感冒指數","ID": 102,"Ascending": 1,"LocalDateTime": "2021-11-11T07:00:00+08:00","EpochDateTime": 1636585200,"Value": 2.0,"Category": "較易發","CategoryValue": 2,"MobileLink": "http://m.weathercn.com/zh/cn/pudong-new-district/74761/weather-forecast/74761?lang=zh-cn","Link": "http://m.weathercn.com/zh/cn/pudong-new-district/74761/weather-forecast/74761?lang=zh-cn" }]print(jmespath.search("[].Name",source))?['晨練指數', '穿衣指數', '感冒指數']總結
以上是生活随笔為你收集整理的Python中jmespath解析提取json数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【性能测试】性能测试的基础理论
- 下一篇: python 安装echarts