current position:Home>After learning JsonPath, your Python interface script is complete

After learning JsonPath, your Python interface script is complete

2022-11-08 06:25:31Rejoice in the testing world

01 Definition of Jsonpath

JsonPath is an easy way to extract parts of a given JSON document.Jsonpath is cross-language, many languages ​​can use jsonpath, such as Javascript, Python and PHP, Java.

The json parsing provided by JsonPath is very powerful. It provides a syntax similar to regular expressions, which can basically satisfy all the json content you want to get.

02 Cases

Let's take a free map interface as an example, the following is the corresponding content of the interface:

Url address: https://ditu.amap.com/service/regeo?longitude=121.04925573429551&latitude=31.315590522490712

Request method: get request

Response data: json data

03 Tool Preparation

pycharm, json online parsing tool, jsonpath online parsing tool

Pycharm is an editing tool for python, you can download this by yourself

Json online parsing tool, the URL is as follows:

https://www.sojson.com/

jsonpath online parsing tool, the URL is as follows:

http://www.e123456.com/aaaphp/online/jsonpath/

Because if there is a lot of json data in the response result, it is not particularly good to see how to extract the corresponding data. At this time, you can use the json online parsing tool, but the characteristic of json data is that the key is wrapped in double quotation marks.

The response value obtained by the Reqeusts library is resp, his resp.json() obtains the type of dictionary, and resp.text obtains a json string, so paste the data obtained by resp.text into the tool for conversion, the code is as follows:

image

Json online conversion is shown in the figure:

image

Jsonpath online parsing works as follows:

image

04 jsonpath syntax

$ represents the value of the entire json data

. Represents a hierarchical relationship, similar to /

of a Windows computer

… Represents a relative hierarchical relationship, similar to //

of Windows computers

[index] If the element is obtained from the list, it is obtained by the index value in the list, and the index value starts from 0.

[0,1] or [start:end] can slice the list to get the corresponding element

[?(@.price)] get key with corresponding attribute

[?(@.price<10)] Get the key with the corresponding attribute and the value of the corresponding attribute has certain characteristics

Requirement 1: Get all the name values ​​in the cross_list

$…name

image

Requirement 2: Get the name value of the second element in the cross_list

$.data.cross_list[1].name

image

If the expression is written incorrectly, it will show that the match is unsuccessful, as shown in the following figure

image

Requirement 3: Get the name value of the first two elements in the cross_list

$.data.cross_list[:2].name

image

Requirement 4: Find out the value corresponding to the key of poi_list contains the value with the direction attribute

$.data.poi_list[?(@.direction)]

image

Requirement 5: Find out the weight in the value of cross_list equal to the value of "130"

$.data.cross_list[?(@.weight==“130”)]

image

How Python uses jsonpath:

First download the third-party library of python

pip install jsonpath

The syntax of Jsonpath is as follows:

jsonpath.jsonpath()

Parameter: json object, jsonpath expression

Return value: list

The code for parsing jsonpath with python is as follows:

image

Remarks: The matching results obtained by jsonpath are stored in a list, so if you want to get the data in it, you must use the index value to get it.

Because of the cross-language and convenience of json, many interfaces written by developers prefer to return json data, so we often encounter parsing json data when doing interface automation testing, such as response data.When making assertions, or in scenarios with interface dependencies, data extraction needs to be done. In both cases, jsonpath parsing will be encountered.So everyone practice and master it well.


Resource Sharing

The complete software testing video learning tutorial below has uploaded the QR code officially certified by CSDN. If you need it, you can get it for free.[100% free guarantee]

insert image description here

insert image description here

copyright notice
author[Rejoice in the testing world],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/312/202211080618473819.html

Random recommended