Once you learn JsonPath, your Python interface script will be complete!

01. Definition of Jsonpath

JsonPath is a simple way to extract parts of a given JSON document. Jsonpath is cross-language, and jsonpath can be used in many languages, such as Javascript, Python, PHP, and 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 obtain.

02. Case

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 easy 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 keys are wrapped in double quotes. The response value obtained by the Reqeusts library is resp, its resp.json() obtains the dictionary type, 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 :

picture

Json online conversion is shown in the figure:

picture

Jsonpath online parsing works as follows:

picture

04. jsonpath syntax

Next, let’s explain the commonly used jsonpath syntax. For other information, please refer to the JSONPATH expressions in the above picture:

$ represents the value of the entire json data

. represents a hierarchical relationship, similar to / on Windows computers.

.. represents a relative hierarchical relationship, similar to Windows computers //

[Index] If the element is obtained from the list, it is obtained through the index value in the list. The index value starts from 0.

[0,1] or [start:end] can slice the list to obtain the corresponding elements.

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

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

Requirement 1: Get the values ​​of all names in cross_list

$..name

picture

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

$.data.cross_list[1].name

picture

If the expression is written incorrectly, the match will be displayed as unsuccessful, as shown in the figure below.

picture

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

$.data.cross_list[:2].name

picture

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

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

picture

Requirement 5: Find the value in the cross_list value where the weight is equal to "130"

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

picture

How to use jsonpath in Python:

First download the third-party library of python

pip install jsonpath

The syntax of Jsonpath is as follows:

jsonpath.jsonpath()

Parameters: json object, jsonpath expression

Return value: list

The code to use python to parse jsonpath is as follows

picture

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

Because of the cross-language and convenience of json, many interfaces written by developers prefer to return json data. Therefore, when we do interface automation testing, we often encounter the problem of parsing json data, such as making assertions on response data. Or when there are interface-dependent scenarios, data extraction needs to be done. In both cases, jsonpath parsing will be encountered. So everyone should practice and master it well.

Finally, I would like to thank everyone who read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software testing interview applet

A software test question bank that has been used by millions of people! ! ! Who is who knows! ! ! The most comprehensive interview test mini program on the Internet, you can use your mobile phone to answer questions, take the subway, bus, and roll it up!

Covers the following interview question sections:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. Web, app, interface automation, 7. Performance testing, 8. Programming basics, 9. HR interview questions, 10. Open test questions, 11. Security testing, 12. Computer basics

Information acquisition method:

Guess you like

Origin blog.csdn.net/myh919/article/details/132736675