简介
响应报文分为JSON格式和字符串格式,往往响应报文中有很多个字段和多层嵌套,如何快速的提取字段key和对应的value值呢?有三种提取方式:jsonpath提取、正则表达式、字符串切片。jsonpath是针对JSON格式的数据提取,后两种主要针对字符串形式的数据提取。
jsonpath
JsonPath是一种用于在JSON数据中定位和提取特定数据的表达式语言。它类似于XPath用于XML的定位和提取,可以帮助我们灵活地从复杂的JSON结构中获取所需的数据。
jsonpath的安装
Terminal终端输入命令行:
pip install jsonpath
也可以使用国内镜像源
pip install jsonpath -i 镜像源地址
以下是国内的几个镜像源地址:
豆瓣:https://pypi.doubanio.com/simple/
阿里云:https://mirrors.aliyun.com/pupi/simple/
华中理工:https://pypi.hustunique.com/simple/
山东理工:https://pypi.sdulinux.org/simple/
中科大:https://pypi.mirrors.ustc.edu.cn/simple/
清华:https://pypi.tuna.tsinghua.edu.cn/simple/
jsonpath的特点
- jsonpath可处理的报文数据类型是字典类型
- 通过jsonpath获取的内容,会以list的形式返回,也就相当于结果以一个值或多个值存在,需要通过list[index]/list[start:end]进一步提取想要的内容。
- 基于jsonpath处理的json数据,需要同时同步list的数据
- jsonpath如果表达式输入错误,则会返回False,故jsonpath的处理结果要么是list 要么是False
jsonpath的基本范式
- 基本语法:
表达式 | 注释 |
---|---|
$ | 根节点,也是所有jsonpath表达式的起点 |
· | 当前节点 |
·· | 递归下级节点 |
- 属性操作
表达式 | 注释 |
---|---|
$.property | 选择指定属性 |
$[‘property’] | 选择指定属性,属性名中带有特殊字符时使用,也可用于平常为了避免出现特殊字符时使用 |
- 数组操作
表达式 | 注释 |
---|---|
$.arrayName[index] | 选择指定节点下索引处的元素 |
$.arrayName[start:end] | 选择指定节点下索引范围内的元素 |
- 过滤器
表达式 | 注释 |
---|---|
$.arrayName[?(@.property == value)] | 根据条件过滤数组元素,这个可以作为找到某个标志性属性后查找同级其他属性 |
$.arrayName[?(@.property > value)] | 同上 |
- 路径组合
表达式 | 注释 |
---|---|
$.parent.child | 选择父节点下的子节点属性 |
$.parent[*].child | 选择父节点下所有子节点的某个属性 |
jsonpath的应用
jsonpath(json_data, “表达式”)
# -*- coding:utf-8 -*-
# @File:test_jsonpath_01.py
# @Date:2025/5/21 22:38
# @Author:wayne
# @description:jsonpath的基础用法import jsonpathdata = {"name": "Alice","age": 25,"email": "alice@example.com","address": {"street": "456 Elm Street","city": "Los Angeles","country": "USA"},"hobbies": ["reading", "traveling", "cooking"],"education": [{"degree": "Bachelor's","major": "Software Engineering","university": "ABC University","year": 2018},{"degree": "Master's","major": "Computer Science","university": "BCD University","year": 2019},{"degree": "Doctor's","major": "Business Administration","university": "XYZ University","year": 2020}],"projects": [{"name": "Project A","description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.","contributors": ["John", "Sarah", "Mike"],"completed": "true"},{"name": "Project B","description": "Nulla vel sagittis elit. Vivamus auctor massa in lacinia pellentesque.","contributors": ["Alice", "David"],"completed": "false"}],"is-active": "true"
}result = jsonpath.jsonpath(data, '$.name')[0]
print("选择指定属性:", result)
result = jsonpath.jsonpath(data, '$.[is-active]')[0]
print("选择指定属性-特殊符号:", result)result = jsonpath.jsonpath(data, '$.education[1]')
print("数组操作-选择指定节点下索引处的元素:", result)
result = jsonpath.jsonpath(data, '$.education[1:]')
print("数组操作-选择指定节点下索引范围内的元素:", result)result = jsonpath.jsonpath(data, '$.education[?(@.year == 2020)]')
print("过滤器1:", result)
result = jsonpath.jsonpath(data, '$.education[?(@.year > 2018)]')
print("过滤器2:", result)result = jsonpath.jsonpath(data, '$.address.city')[0]
print("路径组合-选择父节点下的子节点:", result)
result = jsonpath.jsonpath(data, '$.education[0].year')[0]
print("路径组合-选择父节点下所有子节点的某个属性:", result)
正则表达式提取
在Python中,用 re 模块来进行表达式的操作。
函数 | 作用 |
---|---|
re.search(“正则表达式”, str) | 用于从文本中提取字符串 |
re.findall(“正则表达式”, str) | 用于从文本中提取一个或多个字符串,返回的是一个list列表,包含所有匹配到的字符串 |
应用
# -*- coding:utf-8 -*-
# @File:test_regular_expression.py
# @Date:2025/5/22 18:55
# @Author:wayne
# @description:正则表达式提取文本
import retext = 'result(["username":"tester", "password":"123456" , "token":"6c598762f156457345d2102a6edce274"])'
pattern = '"token":"(.*?)"' # (.*?)是个万能的正则表达式,Jmeter的正则表达式提取也可以使用
match = re.search(pattern, text)
print(match)
# 要通过match的group()方法取出来
print(match.group(0)) # 取出全部匹配的字符串:"token":"6c598762f156457345d2102a6edce274"
print(match.group(1)) # 取出匹配的正则部分的字符串:6c598762f156457345d2102a6edce274text = "There are many kinds of apples,white apple,red apple,green apple." \"White apple sell $1.12 per.Red apple sell $1.25 per.Green apple sells $0.99 per"
pattern = r'\d+\.\d{2}'
match_list = re.findall(pattern, text)
print(match_list) # ['1.12', '1.25', '0.99']
# for match in match_list:
# print(match)
字符串切片
字符串切片用到 split() 函数,split()函数返回的是一个list列表,用法:
split(‘分隔符号’, num)[index],其中num指切片次数,index取结果列表的下标(索引)
应用
# -*- coding:utf-8 -*-
# @File:test_str_split.py
# @Date:2025/5/22 19:51
# @Author:wayne
# @description:字符串切片data = '''result({"name": "Alice","age": 25,"email": "alice@example.com","address": {"street": "456 Elm Street","city": "Los Angeles","country": "USA"},"hobbies": ["reading", "traveling", "cooking"],"education": [{"degree": "Bachelor's","major": "Software Engineering","university": "ABC University","year": 2018},{"degree": "Master's","major": "Computer Science","university": "BCD University","year": 2019},{"degree": "Doctor's","major": "Business Administration","university": "XYZ University","year": 2020}],"projects": [{"name": "Project A","description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.","contributors": ["John", "Sarah", "Mike"],"completed": "true"},{"name": "Project B","description": "Nulla vel sagittis elit. Vivamus auctor massa in lacinia pellentesque.","contributors": ["Alice", "David"],"completed": "false"}],"is-active": "true"
})'''# data_list = data.split('(')
# print(data_list)
# data1 = data_list[1]
# print(data1)
# data2 = data1[:-1]
# print(data2)data = data.split("(")[1][:-1] # [1]:取第2个元素,[:-1]:去掉最后一个字符
print(data)