Json提取器属于JMeter的后置处理器, 所谓后置提取器就是请求结束后, 对响应结果进行变量提取, 提取变量是为了验证变量是否符合预期或者将变量值作为全局变量, 以供其他请求使用.
语法:
JSON的基本语法就是.然后跟要取的字段名,比如要取data,就直接写.data,如果要取msg,就直接写
JSON Extractor使用json path表达式匹配,可以一次取多个变量值。$表示响应的根对象。取子对象或对象的属性用. 取数组里的对象用[],数组索引从0开始。
操作符使用:
JsonPath | 描述 |
---|---|
$ | 根节点 |
@ | 当前节点 |
.or[] | 子节点 |
.. | 选择所有符合条件的节点 |
* | 所有节点 |
[] | 迭代器标示,如数组下标 |
[,] | 支持迭代器中做多选 |
[start:end:step] | 数组切片运算符 |
?() | 支持过滤操作 |
() | 支持表达式计算 |
Json提取器
Json 提取器参数介绍:
Names of created variables:接收值的变量名,多个变量时用分号分隔
Json path:json path表达式,多个表达式用分号分隔
Match no: 0随机;n取第几个匹配值;-1匹配所有,后续引用 变量名_N 取第N个值
Compute comcatemation var(suffix_ALL):如果发现许多结果,插件将使用" , "分隔符将它们连接起来,并将其存储在名为<variable name>_ALL的var中
Default values: 默认值,匹配不到值的时候取该值
Json提取器语法完整说明详见
https://github.com/json-path/JsonPath
Json example:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
使用JsonPathTester来测试书写的Json提取器Expression是否能正常工作
1. 获取某一层中value:
获取title
这个key的value
$.store.book[0].title
2. 获取列表下全部某一个元素的value:
获取price
这个key的所有value值
$.store.book[*].title
使用*
号意味着获取所有列表元素
此种情况下返回为一个list,那么如果此list被放入参数var
中,我们如果想获取var中某一个值譬如12.99
该如何使用呢?答案就是在参数名后边加_n
,其中n为编号,从1开始;
${var_2}
即可获取到列表中第二个元素即12.99
;
3.提取某个固定条件下的value
获取title
这个key的value在固定条件下
$.store.book[?(@.author=='Nigel Rees')].title
4.提取固定条件下多个value
5.获取前or 后第N个value
.result.records[2].id 是排除前两条数据;.result.records[-2].id 是排除后两条数据;
从上示例中可以看出, 参数名称, 表达式, 默认值一次都有多个,用分号隔开
github上给出了较为丰富的用法, 大家可以自己动手去尝试下
JsonPath | Result |
---|---|
$.store.book[*].author | The authors of all books |
$..author | All authors |
$.store.* | All things, both books and bicycles |
$.store..price | The price of everything |
$..book[2] | The third book |
$..book[-2] | The second to last book |
$..book[0,1] | The first two books |
$..book[:2] | All books from index 0 (inclusive) until index 2 (exclusive) |
$..book[1:2] | All books from index 1 (inclusive) until index 2 (exclusive) |
$..book[-2:] | Last two books |
$..book[2:] | Book number two from tail |
$..book[?(@.isbn)] | All books with an ISBN number |
$.store.book[?(@.price < 10)] | All books in store cheaper than 10 |
$..book[?(@.price <= $['expensive'])] | All books in store that are not "expensive" |
$..book[?(@.author =~ /.*REES/i)] | All books matching regex (ignore case) |
$..* | Give me every thing |
$..book.length() | The number of books |
Json表达式书写方式还是需要大家在工作中多加练习, 方能熟练掌握众多组合用法的精髓.
Json提取器提取出来的变量通常可以放在用户自定义变量(或者给Beanshell对象)给其他多个请求使用,从个人喜好来看,如果请求返回结果是json类型,使用json提取器比正则表达式更加方便, 效率方面也会更好.
踩过的坑:
当json提取器要提取两个值(或两个以上)的时候,一定要填写默认值,不然会报错“Mismatch between number of variables, json expressions and default values”
总结:
测试是一门技术, 更是一门艺术. 也许你今天拥有的技术, 明天就会被淘汰. 同时需要我们开拓思维和眼界, 积极拥抱变化, 学习新知识, 新方法,新技能, 计算机领域讲究的是实践, 学习更要讲究方式方法. 学习和动手一定要结合, 光看不练,犹如看武功秘籍, 是永远成不了武功大侠的.
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有