Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

JMeter - JSON variable in a ForEach Controller

5.00/5 (1 vote)
4 Feb 2022CPOL 16.4K   92  
How to read a JSON variable from a ForEach Controller in JMeter
In this tip, you will learn how to read the values from a JSON variable in a ForEach Controller in JMeter.

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:

JavaScript
{
   "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:

Image 1

This will return you an array on json:

Image 2

Know you want to access each property value in the array using a Foreach Controller.

Image 3

You cannot access the properties doing this:

JavaScript
${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.

Image 4

JavaScript
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.

Image 5

Here is a JMeter test that demonstrates how to do it.

History

  • 27th January, 2022: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)