一、JSONPATH的介绍使用
复杂json结构数据的提取,对于复杂的json数据,如果想快速提取,那么可以采用jsonpath的技术来做
1. jsonpath的基本规则
在线调试工具: http://www.e123456.com/aaaphp/online/jsonpath/
基本规则: https://goessner.net/articles/JsonPath/index.html#e2
2. python中jsonpath安装
#windows
pip install jsonpath
# mac
python3 -m pip install jsonpath
二、JSONPATH的封装,读取JSON值
import jsonpath
from jsonpath_rw import Index, Fields
from jsonpath_rw_ext import parse
from common.logger import GetLogger
logger = GetLogger.get_logger()
# 提取json的值
def extract_json(json_object, express, index=0):
res = jsonpath.jsonpath(json_object, express)
try:
if res:
# 如果index小于0,则认为你是要所有的匹配结果
if index < 0:
return res
# 如果不小于0,那么你传几,就代表你要的是匹配结果的某一个
else:
return res[index]
logger.info(f'通过{express}提取到的结果是:{res}')
except:
logger.exception(f'通过表达式{express}没有提取到值!')
if __name__ == '__main__':
s = { "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
}
}
}
# 注意:jsonpath一旦能匹配到数据,不管数据是几个,返回结果都是个列表
# 注意:jsonpath一旦匹配不到数据,那么结果是False
# res = jsonpath.jsonpath(s,'$..color1')
res = extract_json(s,'$..title',3)
print(res)
三、JSONPATH实现更改JSON值封装
1、jsonpath实现json数据替换
针对添加接口编写数据驱动方式的测试用例,使用excel来存储他的测试数据,但是该接口参数很多,如果按照一列对应一个参数的话,不好维护;
那么可否针对要测试的字段作为一列数据,至于接口其他的没有被测试的字段全部采用接口的默认
值;
#windows
pip install jsonpath-rw
pip install jsonpath-rw-ext
# mac
python3 -m pip install jsonpath-rw
python3 -m pip install jsonpath-rw-ext
封装一个方法来实现json数据的替换
import jsonpath
from jsonpath_rw import Index, Fields
from jsonpath_rw_ext import parse
from common.logger import GetLogger
logger = GetLogger.get_logger()
# 提取json的值
def extract_json(json_object, express, index=0):
res = jsonpath.jsonpath(json_object, express)
try:
if res:
# 如果index小于0,则认为你是要所有的匹配结果
if index < 0:
return res
# 如果不小于0,那么你传几,就代表你要的是匹配结果的某一个
else:
return res[index]
logger.info(f'通过{express}提取到的结果是:{res}')
except:
logger.exception(f'通过表达式{express}没有提取到值!')
# 修改JSON的值
def update_value_to_json(json_object, json_path, new_value):
json_path_expr = parse(json_path)
# print(json_path_expr)
for match in json_path_expr.find(json_object):
# print(match)
path = match.path
print(path)
if isinstance(path, Index):
match.context.value[match.path.index] = new_value
elif isinstance(path, Fields):
match.context.value[match.path.fields[0]] = new_value
return json_object
if __name__ == '__main__':
s = { "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
}
}
}
# 注意:jsonpath一旦能匹配到数据,不管数据是几个,返回结果都是个列表
# 注意:jsonpath一旦匹配不到数据,那么结果是False
# res = jsonpath.jsonpath(s,'$..color1')
res = extract_json(s,'$..title',3)
print(res)
# 修改s这个json里的第一个category
s = update_value_to_json(s,'$..book[0].category','aaaaaaaaaa')
print(s)