Click here to Skip to main content
15,611,367 members
Articles / Programming Languages / Javascript
Posted 27 Jan 2022

Tagged as


3 bookmarked

JMeter - JSON variable in a ForEach Controller

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
4 Feb 2022CPOL
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.


I will show you how to access the property values in json variable on JMeter.


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:

         "name":"fuchsia rose",
         "name":"true red",
         "name":"aqua sky",
         "name":"blue turquoise",
      "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:

${} ${}

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

def data2 = vars.get("d");

def json = com.jayway.jsonpath.JsonPath.parse(data2);
def name ='$.name');
def id ='$.id');

vars.put("id", String.valueOf(id));

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.


  • 27th January, 2022: Initial version


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

Written By
Team Leader
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA5-Feb-22 7:15
professionalȘtefan-Mihai MOGA5-Feb-22 7:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.