Prerequisite: In the interface Automation test, there is a dependency: test_02 the value of a request parameter, you need to rely on test_01 to return the data of a field in the result, so we need to get the value of the specific field in the return data first. Use this to Python in JSONPATH-RW library
1. Download and install
Pip Install JSONPATH-RW
2. Import
From JSONPATH_RW import Jsonpath,parse
3. Example Introduction
1. The match data is returned, but we want the value data
jsonpath_expr = Parse ('Foo[*].baz') Data= {'Foo': [{'Baz':'News'}, {'Baz':'Music'}]}Print([Match forMatchinchjsonpath_expr.find (data)]) Run Result: [Datumincontext (Value='News', Path=fields ('Baz'), Context=datumincontext (value={'Baz':'News'}, Path=<jsonpath_rw.jsonpath.index object at 0x025ca850>, Context=datumincontext (value=[{'Baz':'News'}, {'Baz':'Music'}], Path=fields ('Foo'), Context=datumincontext (value={'Foo': [{'Baz':'News'}, {'Baz':'Music'}]}, Path=this (), Context=none))), Datumincontext (value='Music', Path=fields ('Baz'), Context=datumincontext (value={'Baz':'Music'}, Path=<jsonpath_rw.jsonpath.index object at 0x025ca770>, Context=datumincontext (value=[{'Baz':'News'}, {'Baz':'Music'}], Path=fields ('Foo'), Context=datumincontext (value={'Foo': [{'Baz':'News'}, {'Baz':'Music'}]}, Path=this (), Context=none)))]
2. Get the matching data match.value
jsonpath_expr = Parse ('Foo[*].baz') Data= {'Foo': [{'Baz':'News'}, {'Baz':'Music'}]}Print([Match.value forMatchinchJsonpath_expr.find (data)])
Operation Result:
[' News ', ' music ']
3.Match.value return data is a list, we want to get a specific value
jsonpath_expr = Parse ('Foo[*].baz') Data= {'Foo': [{'Baz':'News'}, {'Baz':'Music'}]}Print([Match.value forMatchinchjsonpath_expr.find (data)] [0]) Running results: News
PYTHON3:JSONPATH-RW working with JSON objects