python interface automation 38-jsonpath extracts interface return value

Foreword

The json data returned by the interface needs to be asserted after taking the value. This article uses jsonpath to extract the data returned by the interface.

Interface returns data

The interface returns a json type data. The following data is a very common data structure.

{
"code": 0,
"msg": "success!",
"data": [{
"id": 154,
"create_time": "2021-01-20 22:38:16",
"update_time": "2021-01-20 22:38:16",
"goodsname": ""Selenium Getting Started to Mastering to Giving Up"",
"goodscode": "sp_210001",
"merchantid": "",
"merchantname": "",
"goodsprice": 20.0,
"stock": 0,
"goodsgroupid": 0,
"goodsstatus": 1
}, {
"id": 1,
"create_time": "2021-01-17 15:14:25",
"update_time": "2021-01-20 22:21:51",
"goodsname": ""JMeter Beginner to Master"",
"goodscode": "sp_100049",
"merchantid": "10001",
"merchantname": "Youyou Academy",
"goodsprice": 100.0,
"stock": 1,
"goodsgroupid": 1,
"goodsstatus": 1
}, {
"id": 150,
"create_time": "2021-01-19 23:43:47",
"update_time": "2021-01-19 23:43:47",
"goodsname": ""Cypress Beginner to Master"",
"goodscode": "sp_10002232",
"merchantid": "1000122",
"merchantname": "Youyou Academy",
"goodsprice": 49.9,
"stock": 100,
"goodsgroupid": 0,
"goodsstatus": 1
}, {
"id": 148,
"create_time": "2021-01-19 23:42:20",
"update_time": "2021-01-19 23:42:20",
"goodsname": ""Appium Beginner to Master"",
"goodscode": "sp_426001",
"merchantid": "42601",
"merchantname": "Youyou Academy",
"goodsprice": 99.9,
"stock": 100,
"goodsgroupid": 0,
"goodsstatus": 1
}, {
"id": 147,
"create_time": "2021-01-19 22:22:41",
"update_time": "2021-01-19 22:22:41",
"goodsname": ""Getting Started to Mastering pytest"",
"goodscode": "sp_100119",
"merchantid": "",
"merchantname": "",
"goodsprice": 10.0,
"stock": 0,
"goodsgroupid": 0,
"goodsstatus": 1
}]
}

By.Getting child nodes

For the basics of getting started with jsonpath, you can check out the previous article https://www.cnblogs.com/yoyoketang/p/13216829.html

For relevant syntax, please refer to the table below

Xpath JSONPath Description
/ $ Follow node
. @ Current node
/ . or [] Get the child node
.. n/a Get the parent node JsonPath does not support
// .. Relative nodes select all regardless of position. Conditions that match the condition
* * Match all element nodes
[] [] Iterator mark (you can do simple iteration operations in it, such as under the array mark, select value according to content, etc.)
[,] [,] Supported Make multiple selections in iterator
[] ?() Support filtering operation
n/a () Support expression calculation
() n/a Grouping, JsonPath does not support
import jsonpath

s = {
"code": 0,
"msg": "success!",
"data": [{
"id": 154,
"create_time": "2021-01-20 22:38:16",
"update_time": "2021-01-20 22:38:16",
"goodsname": ""Selenium Getting Started to Mastering to Giving Up"",
"goodscode": "sp_210001",
"merchantid": "",
"merchantname": "",
"goodsprice": 20.0,
"stock": 0,
"goodsgroupid": 0,
"goodsstatus": 1
}, {
"id": 1,
"create_time": "2021-01-17 15:14:25",
"update_time": "2021-01-20 22:21:51",
"goodsname": ""JMeter Beginner to Master"",
"goodscode": "sp_100049",
"merchantid": "10001",
"merchantname": "Youyou Academy",
"goodsprice": 100.0,
"stock": 1,
"goodsgroupid": 1,
"goodsstatus": 1
}, {
"id": 150,
"create_time": "2021-01-19 23:43:47",
"update_time": "2021-01-19 23:43:47",
"goodsname": ""Cypress Beginner to Master"",
"goodscode": "sp_10002232",
"merchantid": "1000122",
"merchantname": "Youyou Academy",
"goodsprice": 49.9,
"stock": 100,
"goodsgroupid": 0,
"goodsstatus": 1
}, {
"id": 148,
"create_time": "2021-01-19 23:42:20",
"update_time": "2021-01-19 23:42:20",
"goodsname": ""Appium Beginner to Master"",
"goodscode": "sp_426001",
"merchantid": "42601",
"merchantname": "Youyou Academy",
"goodsprice": 99.9,
"stock": 100,
"goodsgroupid": 0,
"goodsstatus": 1
}, {
"id": 147,
"create_time": "2021-01-19 22:22:41",
"update_time": "2021-01-19 22:22:41",
"goodsname": ""Getting Started to Mastering pytest"",
"goodscode": "sp_100119",
"merchantid": "",
"merchantname": "",
"goodsprice": 10.0,
"stock": 0,
"goodsgroupid": 0,
"goodsstatus": 1
}]
}

code = jsonpath.jsonpath(s, '$.code')
print(code) # Output result [0]
msg = jsonpath.jsonpath(s, '$.msg')
print(msg) # Output result ['success!']
names = jsonpath.jsonpath(s, '$..goodscode')
print(names) # Output results ['sp_210001', 'sp_100049', 'sp_10002232', 'sp_426001', 'sp_100119']
no = jsonpath.jsonpath(s, '$..yoyo')
print(no) # If not found, the result is False

$. is to get the child node. If it is not the current node, you can use $.. to get the value of the relative node and get the matching values of all descendant nodes.

list value

1. Get the first piece of data in data based on the subscript. The subscript starts from 0.

data1 = jsonpath.jsonpath(s, '$.data[0]')
print(data1)

# return
[{'id': 154, 'create_time': '2021-01-20 22:38:16', 'update_time': '2021-01-20 22:38:16', 'goodsname': '"Getting Started with Selenium" To master to give up》', 'goodscode': 'sp_210001', 'merchantid': '', 'merchantname': '', 'goodsprice': 0.0, 'stock': 0, 'goodsgroupid': 0, 'goodsstatus' : 1}]

2. Get the goodsname of the first piece of data under data

data2 = jsonpath.jsonpath(s, '$.data[0].goodsname')
print(data2)

# ['"Selenium Getting Started to Mastering to Giving Up"']

3. To take out the first two pieces of data, you can use the slice of list to get the value [:2]

data3 = jsonpath.jsonpath(s, '$.data[:2]')
print(data3)

4. Take out the last two pieces of data and use list slicing to get the value [-2:]

data4 = jsonpath.jsonpath(s, '$.data[-2:]')
print(data4)

5. Take out the second to last piece of data. The [-2] index is not supported here, but slicing [-2:-1] can be used.

data5 = jsonpath.jsonpath(s, '$.data[-2:-1]')
print(data5)

6. Take out the 1st and 3rd data. Multiple values can be separated by commas [0,2]

data6 = jsonpath.jsonpath(s, '$.data[0,2]')
print(data6)

?() filter operator

Filters are logical expressions used to filter arrays. A typical filter would be [?(@.age > 18)] , where @ represents the current project being processed.
More complex filters can be created using the logical operators & amp; & amp; and ||. String literals must be enclosed in single or double quotes ([?(@.name == 'yoyo')] or [?(@.name== "yoyo")] ).

Operator Description
== left equals right (note that 1 is not equal to ‘1’)
!= Not equal to
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal
=~ Match regular expression [?(@.name =~ /foo.*?/i)]
in The left side exists on the right side[?(@.size in ['S', 'M'])]
nin The left side does not exist on the right side
size (array or string) length
empty (array or string) is empty

?()Use of filter expression

?() filter expression. The expression must evaluate to a Boolean value. The expression is generally combined with @ to obtain the current node to filter.

1. Find all product information with a product price greater than 30

# The price is greater than 30
data1 = jsonpath.jsonpath(s, '$.data[?(@.goodsprice > 20)]')
print(data1)

# Goodscode with price greater than 30
goodscodes = jsonpath.jsonpath(s, '$.data[?(@.goodsprice > 20)].goodscode')
print(goodscodes) # ['sp_100049', 'sp_10002232', 'sp_426001']

# Goodsname with price greater than 30
goodsnames = jsonpath.jsonpath(s, '$.data[?(@.goodsprice > 20)].goodsname')
print(goodsnames) # ['《JMeter Beginner to Master》', 'Cypress Beginner to Master》', 'Appium Beginner to Master》]

2. Take out the goodsname corresponding to 'goodscode': 'sp_100049'

# Take out the goodsname corresponding to 'goodscode': 'sp_100049'
name2 = jsonpath.jsonpath(s, '$.data[?(@.goodscode == "sp_100049" )].goodsname')
print(name2) # ['"JMeter Beginner to Master"]

3. Take out the goodsname corresponding to 'goodscode': 'sp_100049' and 'goodscode': 'sp_100119'

# in is included nin does not exist
name3 = jsonpath.jsonpath(s, '$.data[?(@.goodscode in ["sp_100049", "sp_100119"])].goodsname')
print(name3) # ['《JMeter Beginner to Master》', 'pytest Beginner to Master》]

jsonpath online parsing address http://www.atoolbox.net/Tool.php?Id=792
Note: Regular expression filtering is not supported in Python yet.
QQ communication group: 730246532