Reputation: 337
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??
Upvotes: 4
Views: 14716
Reputation: 5907
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:
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);
Upvotes: 1
Reputation: 983
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.
Upvotes: 3
Reputation: 168157
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.
myVar
{"id":"(.+?)", "name":"(.+?)"}
$1$$2$
4. Other option is use it in combination with the Debug Sampler to get JMeter Variables names
So you will be able to refer:
${myVar_g1}
${myVar_g2}
Upvotes: 1