1、 Application scenarios : Interface automation testing , There are parameter dependencies ,A The response parameters of the interface are used as B The request parameters of the interface request the interface , At this time, we need to start from A In the response data of B Get the required request parameters , At this time , It needs to be used jsonpath_rw
2、 Premise : install jsonpath_rw
pip install jsonpath_rw -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
3、 example :
from jsonpath_rw import jsonpath, parse
data={
"Code": 200,
"ErrorMessage": "",
"Data": {
"IsSuccess": true,
"Message": " Quantity updated successfully ",
"StackTrace": null,
"Data": {
"cartType": 0,
"CartId": "a47141fddd8848e1be5a281b25e613b8"
}
}
}
depend='Data.Data.CartId' # Write the data to be found according to the response data structure CartId
json_exe = parse(depend) # parse Used to parse... From a string json object
model = json_exe.find(response_data) # The return is list, But not the value we want
print([match.value for match in model][0])
# Return to the response parameter CartId Of a47141fddd8848e1be5a281b25e613b8
Official document address :GitHub - kennknowles/python-jsonpath-rw: A robust and significantly extended implementation of JSONPath for Python, with a clear AST for metaprogramming.