When you learn this python data extraction artifact, please be ready for promotion

1. What is jsonpath

● JsonPath is an information extraction class library, a tool for extracting specified information from JSON documents, and provides versions in multiple languages, including JavaScript, Python, PHP and Java.

*At the end of the article, receive 10 excellent automation courses*

2. Features

● Can only extract data in JSON format

● The extracted data type is consistent with the original data type

3. Installation

pip install jsonpath

4. Common original characters

original character

describe

$

Represents the root element

@

current element

. or []

child element

..

Recursive search (regardless of the current path, search for eligible data)

*

Wildcard, which means all elements

[]

child element operator

[,]

Support multiple selections in the iterator, multiple keys are separated by commas

[start:end:step]

Array division operation, equivalent to slice

?()

apply filter expression

Five, the use of common metacharacters

● test data

class_info = {"class_one": {
   
       "students": [        {"name": "张一",         "sex": "男",         "age": 18,         "height": 170.5         },        {"name": "张二",         "sex": "女",         "age": 20,         "height": 160.5         },        {"name": "张三",         "sex": "男",         "age": 18,         "height": 170.5         },    ],    "teacher": {
   
           "name": "李小二",        "sex": "男",        "age": 30,        "height": 185.5,        "teacher":"递归搜索测试"    }}}

● $: root element​​​​​​

import jsonpath#获取根元素下所有数据,2种写法一样#.的作用等同于[]表示子元素result = jsonpath.jsonpath(class_info, '$.*')result2 = jsonpath.jsonpath(class_info, '$[*]')print(result)print(result2)输出:[{'students': [{'name': '张一', 'sex': '男', 'age': 18, 'height': 170.5}, {'name': '张二', 'sex': '女', 'age': 20, 'height': 160.5}, {'name': '张三', 'sex': '男', 'age': 18, 'height': 170.5}], 'teacher': {'sex': '男', 'age': 30, 'height': 185.5, 'teacher': '递归搜索测试'}}]

● . or []: child element

import jsonpath
#.与[]作用相同后续就只写一个了
result = jsonpath.jsonpath(class_info, '$.class_one.students')
print(result)

result = jsonpath.jsonpath(class_info, '$[class_one][students]')
print(result)
输出:
[[{'name': '张一', 'sex': '男', 'age': 18, 'height': 170.5}, {'name': '张二', 'sex': '女', 'age': 20, 'height': 160.5}, {'name': '张三', 'sex': '男', 'age': 18, 'height': 170.5}]]

● [,]: supports multiple selection in the iterator, multiple keys are separated by commas

import jsonpath
#递归查找包含teacher 或者 name的值
# ..:表示递归查找,可以搜索到该json下所有符合条件的数据
result = jsonpath.jsonpath(class_info, '$..[teacher,name]')
print(result)
输出:
[{'sex': '男', 'age': 30, 'height': 185.5, 'teacher': '递归搜索测试'}, '张一', '张二', '张三', '递归搜索测试']

#获取students下第0个和第2个元素
re = "$..students[0,2]"
result = jsonpath.jsonpath(class_info,re)
print(result)
输出:
[{'name': '张一', 'sex': '男', 'age': 18, 'height': 170.5}, {'name': '张三', 'sex': '男', 'age': 18, 'height': 170.5}]

● [start:end:step]: array division operation, equivalent to slice, follow the principle of left-closed and right-open

import jsonpath#获取前2位学生的信息,支持下标运算,类似list通过下标取值一样result = jsonpath.jsonpath(class_info, '$.class_one.students[0:2]')print(result)输出:[{'name': '张一', 'sex': '男', 'age': 18, 'height': 170.5}, {'name': '张二', 'sex': '女', 'age': 20, 'height': 160.5}]

● ?(): apply filter expression

import jsonpath#找出年龄大于18的学生result = jsonpath.jsonpath(class_info, '$.class_one.students.[?(@.age>18)]')print(result)输出:[{'name': '张二', 'sex': '女', 'age': 20, 'height': 160.5}]

Six, filter expression

original character

describe

==

Equal to sign, but number 1 is not equal to character 1

!=

not equal to sign

<

less than sign

<=

less than or equal to sign

>

more than the

>=

greater than or equal to symbol

=~

Determine whether it matches the regular expression, such as [?(@.name =~ /foo.*?/i)]

in

Belonging symbol: [?(@.name in['Zhang Er','Zhang San'])]

not in

Exclude symbols: [?(@.name not in ['Zhang Er','Zhang San'])]

&&

Logical AND, for combining multiple filter expressions

II

Logical OR, for combining multiple filter expressions

Seven, the use of filter expressions

● ==: equal to​​​​​​

import jsonpath#下面几个比较的和这个一样就不写了#找出name==张三的学生result = "$.class_one.students.[?(@.name=='张三')]"print(result)输出:[{'name': '张三', 'sex': '男', 'age': 18, 'height': 170.5}]

● in: belongs to the symbol

import jsonpath
#获取name等于张二或者张三
re = "$.class_one.students.[?(@.name in ['张二','张三'])]"
result = jsonpath.jsonpath(class_info,re)
print(result)
输出:
[{'name': '张二', 'sex': '女', 'age': 20, 'height': 160.5}, {'name': '张三', 'sex': '男', 'age': 18, 'height': 170.5}]

#找出name为张二,张三的学生年龄
re = "$.class_one.students.[?(@.name in ['张二','张三'])].age"
result = jsonpath.jsonpath(class_info,re)
print(result)
输出:[20, 18]

● &&: Logical AND, used to combine multiple filter expressions​​​​​​​​

import jsonpathre = "$..students[?(@.name=='张三' && @.age==18)]"result = jsonpath.jsonpath(class_info,re)print(result)输出:[{'name': '张三', 'sex': '男', 'age': 18, 'height': 170.5}]

● || Logical OR, used to combine multiple filter expressions​​​​​​​​

import jsonpath#获取name等于张三或者age等于18的学生re = "$..students[?(@.name=='张三' || @.age==18)]"result = jsonpath.jsonpath(class_info,re)print(result)输出:[{'name': '张一', 'sex': '男', 'age': 18, 'height': 170.5}, {'name': '张三', 'sex': '男', 'age': 18, 'height': 170.5}]

● not in : exclude symbols​​​​​​​​

import jsonpath#name不等于'张一','张三'的学生re = "$..students[?(@.name not in ['张一','张三'])]"result = jsonpath.jsonpath(class_info,re)print(result)输出:[{'name': '张二', 'sex': '女', 'age': 20, 'height': 160.5}]

Finally: If you don't want to experience the feeling of not being able to find information, no one answering questions, and giving up after a few days of learning, here I will share with you some learning resources for software testing, hoping to give you some guidance on the way forward. Come to help, friends can get it for free if they need it 【保证100%免费】

Collection of software testing interview questions

Our advanced study of automated testing must be to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. After completing this set of interview materials, I believe everyone can find a satisfactory job.

How to get the video file:

Guess you like

Origin blog.csdn.net/m0_75277660/article/details/130773093