4
votes

I am using json path extractor in jmeter. However it seems I can only extract one value each time per extractor. i.e Name:- variable name, JSON path:- $.id

If I need to pull out say id, name from json and want to store in two different variables can I use a single json extractor. something like Name :- id_value, name_value JSON path:- $.id, $.name

json string

 {"id":"blah id", "name":"blah name"}

its quite straight forward to use two extractors to get two values. However is it possible in a single extractor??

3

3 Answers

3
votes

If you are using a version of jmeter that supports JSON Extractor, you can extract multiple values and store them in multiple variables using only one JSON Extractor post processor.

Note that variable names, JSON path expressions and default values have to be separated by a semi-colon ";" and must match each others numbers(3 variable names = 3 expressions and 3 default values) as explained in the user manual.

1
votes

As for current version 1.2.0 of JSON Path Extractor it isn't something supported.

However you can get it done with the Regular Expression Extractor.

  1. Add Regular Expression Extractor as a child of the request which returns your JSON string
  2. Configure it as follows:
    • Reference Name: anything meaningful, i.e. myVar
    • Regular Expression: {"id":"(.+?)", "name":"(.+?)"}
    • Template: $1$$2$
  3. To visualize results you can use View Results Tree listener which has built-in Regex Tester.

Regex Tester 4. Other option is use it in combination with the Debug Sampler to get JMeter Variables names

Debug Sampler

So you will be able to refer:

  • ID - as ${myVar_g1}
  • Name - as ${myVar_g2}
1
votes

This is an old question but I was struggling with this problem and this is my solution. In the case asked here

{"id":"blah id", "name":"blah name"}

you can solve it with JSON Extractor Post-processor. In my experience, I wasn't able to use two Post-processors. This is the post-processor that works to obtain id and name:

enter image description here

In JSON Path expressions you write:

$.["id", "name"]

Then testVar2 will have the value (from debug sampler):

testVar2_1={"name":"blah name","id":"blah id"}
testVar2_matchNr=1

However I wasn't able to use a JSON Extractor post-processor for a json response like:

{"id":"blah id", "other" : {"name":"blah name"}}

In this case I had to use a BeanShell PostProcessor with a script like this:

import net.minidev.json.parser.JSONParser;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONArray;

JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);

String jsonString = prev.getResponseDataAsString();
JSONObject jsonObj = (JSONObject) p.parse(jsonString);
String idString = (String) jsonObj.get("id");
JSONObject otherJSONObject = jsonObj.get("other");

String nameString = (String) otherJSONObject.get("name");

log.info("ID:" + idString);
log.info("NAME:" + nameString);

vars.put("IDVAR", idString);
vars.put("NAMEVAR", nameString);