0
votes

I'm dealing with some problems with Jmeter. I do an HTTP request to an API, this API answer me in JSON, so I use a JSON extractor to extract the information that I need (I use the JSON path expression "$.asset_host" to get it and store it in a variable named "data"). So now I have a variable which is an array named "data" with data[0] = asset_host.

The problem is that I need to use this information in the next HTTP request but I don't know how to get asset_host from "data" without using a foreach controller. I tried ${data[0]}, ${data}[0], and ${data_0} but it doesn't work.

Do someone know how to get asset_host value at key 0 of array data please ?

EDIT

Here is the JSON response:

{
    "chat_web_socket_port": "9009",
    "assets_server": "\/\/assets.local",
    "chat_web_socket_host": "chat",
    "chat_web_socket_secure": false
}

Here the JSON extractor

EDIT_2

Ok I found why it doesn't work. The JSON response give me "//assets.local" so if I use it in the next request as host it puts "//assets.local" as host that's why it doesn't work. I will have to manipulate string to remove the "//".

EDIT_3

Ok just to finish this post, I used a JSR223 sampler to manipulate the string and remove the "//". I put the code here, maybe it will help someone esle in the future.

String assets = vars.get("assetServerHost_1");
String newAssets = assets.replace('//', '');
vars.put("hostForAssets", newAssets);

It's changing "//assets.local" to "assets.local"

Thank you for reading, and for your help.

2

2 Answers

0
votes

It's hard to say what's wrong without seeing your test plan, your JSON Extractor configuration and the JSON response, there are too many possible failure reasons, the most common one is incorrect placement of the JSON Extractor (see JMeter Scoping Rules user manual chapter for more details)

Blind shots:

  1. Put 0 into "Match No: field:

    enter image description here

this way you will be able to refer the desired value as ${data}

  1. If you have -1 there - then most probably you have ${data_1} variable and ${data_matchNr} variable holding the number of matches

You can check which variables are generated by the JSON extractor using Debug Sampler and View Results Tree listener combination

enter image description here

0
votes

can you check how the data looks in Debug Sampler?

my guess is: if $.asset_host is an array then jmeter will create individual variables as data_1, data_2, .... data_n First element of array will be data_1

EDITED BELOW

Debug Sampler is out-of-box in Jmeter. Details here.

I used this sample JSON

{
"name": "John",
"age": 30,
"cars": [
    "Ford",
    "BMW",
    "Fiat"
],
"asset_host": "test string"
}

My JSON Extractor configuration enter image description here

Do note that Match No should be equal to 0

Below is the extracted value as seen in Debug Sampler enter image description here