7

JMeter - JSON variable in a ForEach Controller

 2 years ago
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.
neoserver,ios ssh client

Introduction

I will show you how to access the property values in jsonvariable 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
Shrink ▲   Copy Code
{
   "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
Copy Code
${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
Copy Code
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

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK