Python3中JsonPath用法
最近在学习断言机制,看到了别人用JsonPath进行匹配后断言,它相比assert更加的简洁,灵活度也更高,本内容依赖Python3语言记录;
一、JsonPath介绍:
首先,JsonPath 是一种信息抽取类库,是从JSON文档中抽取指定信息的工具,提供多种语言实现版本,包括:Javascript, Python, PHP 和 Java。直白点的话就是独立的可以配合多种语言进行匹配的目标值的一种类库,和jmeter中的jsonPath匹配方式很像(具体参考此文章:https://www.cnblogs.com/Zhan-W/p/10985816.html )。优点之一就是 数据可以通过交互方式从客户端上的JSON结构提取,不需要特殊的脚本
二、语法说明
(根据官文结合Xpath进行对比说明)
JSONPath 表达式总是以与 XPath 表达式与 XML 文档结合使用的相同方式引用 JSON 结构。由于 JSON 结构通常是匿名的并且不一定具有“根成员对象”,因此 JSONPath 假定$分配给外层对象的抽象名称。
JSONPath 表达式可以使用点符号
$.store.book[0].title
或括号 -符号
$['store']['book'][0]['title']
对于输入路径。内部或输出路径将始终转换为更通用的括号 -符号。
JSONPath 允许将通配符* 用于成员名称和数组索引。它借用后代从操作者“..” E4X和阵列切片语法提案[start:end:step]从EcmaScript的4。
底层脚本语言的表达式(<expr>)可以用作显式名称或索引的替代,如
$.store.book[(@.length-1)].title
对当前对象使用符号“@”。过滤器表达式通过语法的支持?(<boolean expr>),如
$.store.book[?(@.price < 10)].title
这是 JSONPath 语法元素与其对应的 XPath 的完整概述和并排比较
三、举例说明,结合实列对比说明:
官文给的实列内容:
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
}
}
}
匹配结果:
四、下边以一个实际的订单数据为例,举例说明:
import jsonpath
testdata = {
"code": 0,
"status": 1,
"data": { "list": [ { "stockOutId": "1467422726779043840", "orderId": "1467422722362441728", "id": "1467422722362441728", "stockOutStatus": { "name": "待出库", "value": 0, "description": "待出库" }, "orderStatus": { "name": "待付款", "value": 0, "description": "待付款" }, "orderPayType": { "name": "货到付款", "value": 1, "description": "货到付款" }, "orderDeliveryWay": { "name": "物流配送", "value": 0, "description": "物流配送" }, "orderTradeType": { "name": "即时到帐交易", "value": 4, "description": "即时到帐交易" }, "stockOutType": { "name": "制单出库", "value": 1, "description": "制单出库" }, "creator": 9002257, "reviser": 9002257, "createTime": "2021-12-05 17:16:55", "shippingFee": 0, "totalAmount": 629, "sumProductPayment": 629, "currency": "RMB", "toFullName": "张德天", "toAddress": None, "toFullAddress": "湖北省武汉市洪山区街道口", "storageName": "初始仓库", "orderTime": "2021-12-05 17:16:55", "isSplit": 0, "packageNum": "1/1", "stockOutCreateTime": "2021-12-05 17:16:56", "stockOutToFullName": "张德天", "stockOutToFullAddress": "湖北省武汉市洪山区街道口", "creatorName": "监狱账号联系人", "stockOutTotalQuantity": 5, "stockOutTotalAmount": 629, "reviserName": "监狱账号联系人", "outNo": "WB20211206171552638541", "tenantId": 363635127, "orderNo": "WD20211205622150001", "stockOutOutNo": "WB20211206171552638541", "toReceiveTime": "2021-12-05 17:16:55", "stockOutBespeakTime": "2021-12-05 17:15:00", "stockOutNo": "CK2021120562128001" }, { "stockOutId": "1467512423597473792", "orderId": "1467512420523048960", "id": "1467512420523048960", "stockOutStatus": { "name": "待出库", "value": 0, "description": "待出库" }, "orderStatus": { "name": "待发货", "value": 1, "description": "待发货" }, "orderPayType": { "name": "货到付款", "value": 1, "description": "货到付款" }, "orderDeliveryWay": { "name": "物流配送", "value": 0, "description": "物流配送" }, "orderTradeType": { "name": "即时到帐交易", "value": 4, "description": "即时到帐交易" }, "stockOutType": { "name": "销售出库", "value": 0, "description": "销售出库" }, "creator": 9002257, "reviser": 9002257, "createTime": "2021-12-05 23:13:20", "reviseTime": "2021-12-05 23:14:00", "status": 0, "storageId": 101888, "no": "WD20211205836010001", "sumProductPayment": 880.6, "refundAmount": 0, "buyerFeedback": "发新鲜货,尽快发货", "buyerLevel": "", "sellerId": "3e703f8e28c54d86b3e67fecc1dbff67", "sellerName": "监狱公司-2416", "toDivisionCode": "长安区", "toTownCode": "", "shopName": "监狱公司-2416", "toAddress": "火车站", "toFullAddress": "河北省石家庄市长安区火车站", "payStatus": "未支付", "orderPayWay": 0, "valetPayStatus": 0, "storageName": "初始仓库", "stockOutCreateTime": "2021-12-05 23:13:21", "stockOutToFullName": "张德天", "stockOutToFullAddress": "河北省石家庄市长安区火车站", "creatorName": "监狱账号联系人", "stockOutTotalQuantity": 7, "stockOutTotalAmount": 880.6, "orderNo": "WD20211205836010001", "stockOutOutNo": "2112058359100014846", "stockOutNo": "CK2021120583602001" } ], "pageIndex": 0, "pageSize": 50, "total": 2, "pageCount": 1, "data": { "addRepairOrder": True, "cancelOrder": True } }, "message": "操作成功。", "isSuccessed": True } # 匹配message值 # 常规匹配: print(testdata["message"])
# jsonpath匹配(取出来是个列表) print(jsonpath.jsonpath(testdata, '$..message'))
# 去列表 print(jsonpath.jsonpath(testdata, '$..message')[0]) # 匹配list值 print(jsonpath.jsonpath(testdata, '$..list')[0]) # 匹配stockOutId值 print(jsonpath.jsonpath(testdata, '$..stockOutId')) # 匹配stockOutStatus值 print(jsonpath.jsonpath(testdata, '$..stockOutStatus'))
# 匹配data下所有的元素 print(jsonpath.jsonpath(testdata, '$.data.*')) # 匹配data下list所有的orderId值 print(jsonpath.jsonpath(testdata, '$.data.list[*].orderId')) print(jsonpath.jsonpath(testdata, '$..orderId')) # 匹配data下list中倒数第一个orderId值 print(jsonpath.jsonpath(testdata, '$.data.list[*].orderId')[-1]) # 匹配data--list下所有的stockOutType值 print(jsonpath.jsonpath(testdata, '$.data..stockOutType')) print(jsonpath.jsonpath(testdata, '$..stockOutType')) # 匹配data--list下第二个stockOutType中的description值 print(jsonpath.jsonpath(testdata, '$.data..stockOutType.description')[1]) # 匹配data--list下所有orderTradeType中所有的name值
print(jsonpath.jsonpath(testdata, '$..orderTradeType.name')) # 匹配data--list中包含OutOutNo的所有列表值,并返回stockOutOutNo值 print(jsonpath.jsonpath(testdata, '$..list[?(@.stockOutOutNo)].stockOutOutNo')) # 匹配data--list下sumProductPayment>800的所有值,是把list中满足条件的值列出来 print(jsonpath.jsonpath(testdata, '$..list[?(@.sumProductPayment>800)]')) # 匹配data--list下sumProductPayment>800的所有值,并取出sumProductPayment的值 print(jsonpath.jsonpath(testdata, '$..list[?(@.sumProductPayment>800)].sumProductPayment')) # 匹配orderPayType的所有值 print(jsonpath.jsonpath(testdata, '$..orderPayType')) # 匹配orderPayType中所有的valve值 print(jsonpath.jsonpath(testdata, '$..orderPayType.*')) # 匹配orderPayType返回的多个结果中的第一个 print(jsonpath.jsonpath(testdata, '$..orderPayType')[0]) # 匹配orderPayType中的description值 print(jsonpath.jsonpath(testdata, '$..orderPayType.description'))
# 更多玩法姿势我也还在探索中,各位路过的大佬有新玩法可以留言,学习下,不胜感激!
匹配结果:
学如逆海行舟,不进则退。