roschach
roschach

Reputation: 9336

JMeter pass JSON response value to next request

I am using JMETER to test a web app.

First I perform a http GET request which returns a JSON array such as:

[
  {
    "key1": 
    {
      "subKey": 
      [
        9.120968,
        39.255417
      ]
    },
    key2 : 1

  },
  {
    "key1": 
    {
      "subKey": 
      [
        9.123852,
        39.243237
      ]
    },
    key2 : 10
  }

]

Basically I want to take randomly one element, take the elements of key1 and create 2 variables in JMeter that will be used for the next query (if randomly it is not possible than just the 1st element).

I tried using JSON Extractor with the following settings (the example shows a single variable case):

enter image description here

and in the next http GET request referencing the parameter as ${var1}.

How to set JSON Extractor to extract a value, save into a JMeter variable to be used in the next http GET request?

Upvotes: 1

Views: 6025

Answers (2)

Dmitri T
Dmitri T

Reputation: 168072

  1. Correct JSON Path query would be something like:

    $..key1.subKey[${__Random(0,1,)}]
    
  2. You need to switch Apply to value either to Main sample only or to Main sample and sub-samples

    enter image description here

In the above setup:

  • Match No: 0 - tells JMeter to get random value from key1 subkey
  • ${__Random(0,1,)} - gets a random element from the array, i.e. 9.120968 or 39.255417

More information:

Upvotes: 3

Yuri G
Yuri G

Reputation: 1213

"JMeter variable name to use" option that you've switched on there means that you'd be examining the content of this variable INSTEAD of Sample result.

So the fix is obvious: if you intend to extract whatever you extracting from Sample result - change it back to it.

PS If you intend the opposite (process the variable content, not the sample result) - let me know please.

Upvotes: 0

Related Questions