JMeter - JSON variable in a ForEach Controller
source link: https://www.codeproject.com/Tips/5323656/JMeter-JSON-variable-in-a-ForEach-Controller
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Introduction
I will show you how to access the property values in json
variable on JMeter.
Background
While working with JMeter, I discovered that JMeter does not easily determine the value of a property of the json variable. Are you wondering what I mean by that?
Take this example:
You have HTTP request that returns you this json:
{ "page":1, "per_page":6, "total":12, "total_pages":2, "data":[ { "id":1, "name":"cerulean", "year":2000, "color":"#98B2D1", "pantone_value":"15-4020" }, { "id":2, "name":"fuchsia rose", "year":2001, "color":"#C74375", "pantone_value":"17-2031" }, { "id":3, "name":"true red", "year":2002, "color":"#BF1932", "pantone_value":"19-1664" }, { "id":4, "name":"aqua sky", "year":2003, "color":"#7BC4C4", "pantone_value":"14-4811" }, { "id":5, "name":"tigerlily", "year":2004, "color":"#E2583E", "pantone_value":"17-1456" }, { "id":6, "name":"blue turquoise", "year":2005, "color":"#53B0AE", "pantone_value":"15-5217" } ], "support":{ "url":"https://reqres.in/#support-heading", "text":"To keep ReqRes free, contributions towards server costs are appreciated!" } }
And you want to get an array that contains the id
and name
, use a JSON Extractor to get this information:
This will return you an array on json:
Know you want to access each property value in the array using a Foreach Controller.
You cannot access the properties doing this:
${d.id} ${d.name}
Because the JMeter
d
is not a JSON, it is a string.
Using the Code
A workaround for this situation is to use a JSR223 Sampler to do some manipulation. What we can do is read the d
variable and extract the information that we need and add this information to another variables.
def data2 = vars.get("d"); def json = com.jayway.jsonpath.JsonPath.parse(data2); def name = json.read('$.name'); def id = json.read('$.id'); vars.put("id", String.valueOf(id)); vars.put("name",name);
With this, we can access id
and name
in the next HTTP Request.
Here is a JMeter
test that demonstrates how to do it.
History
- 27th January, 2022: Initial version
Recommend
-
47
前言 前端开发中经常涉及到数组的相关操作:去重、过滤、求和、数据二次处理等等。都需要我们对数组进行循环。为了满足各种需求,JS除了提供最简单的for循环,在ES6和后续版本中也新增的诸如:map、filter、some、reduce等实用的方法。因为各个方法作
-
34
A foreach loop runs a block of code for each element of a list. No big whoop,...
-
8
The controller can not access the angular service variable advertisements I have a problem with my Angular Service. I have two controllers and...
-
8
Jmeter-Extract Data from JSON using Json Extractor Reading Time: 3 minutes Jmeter is used to perform load testing. I have already discussed
-
7
How to load JSON from a variable, from an object advertisements I have this code: var browserName = "chrome"; function get...
-
6
Play controller does not accept JSON POST advertisements I am working on Play 1.2.4. I have set up the routes like this: P...
-
3
JMeter 的事务控制器是一个非常方便的工具,用于组织测试的不同部分并确定这些部分将如何出现在报告中。正如 Apache JMe...
-
9
Unable to receive json data in controller with knockout advertisements I am new with knockout and mvc, so I need some help, my question is my dropd...
-
4
How to generate and pass a JavaScript variable from MVC Controller? advertisements I'm trying to fill a JSON object with a...
-
5
一、使用场景json提取器用于提取请求返回结果中的某个值或者某一组值,存储为指定的变量,用于后续的接口调用,使用方法比正则表达式提取器简单,所以如果接口响应数据为json格式的,我们优先使用json提取器。二、使用方法
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK