1

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?

roschach
  • 8,390
  • 14
  • 74
  • 124

2 Answers2

3
  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:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Sorry maybe my question was not clear enough. I want to pick a random element from the most external array so that it will look like `{ "key1": { "subKey": [ 9.120968, 39.255417 ] }, key2 : 1 . }` and from here extract both values from `subKey` – roschach Dec 03 '18 at 15:20
  • Then go for `$..key1.subKey` only – Dmitri T Dec 03 '18 at 15:25
  • I kept the 0 though to extract a random element and it seems correct. – roschach Dec 03 '18 at 15:35
0

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

Yuri G
  • 1,206
  • 1
  • 9
  • 13