I have a Sample JSON response as below:
{
"id": 37,
"merchant_id": "39",
"title": "Parker Pens",
"subtitle": null,
"price": 1000,
"description": null,
"images": [],
"image_thumbs": [],
"options": [{
"code": "color",
"label": "Color",
"extra_info": "",
"values": [{
"label": "Red",
"value": "8"
}, {
"label": "Yellow",
"value": "9"
}, {
"label": "Pink",
"value": "10"
}]
}, {
"code": "size",
"label": "Size",
"extra_info": "",
"values": [{
"label": "Small",
"value": "4"
}, {
"label": "Medium",
"value": "5"
}, {
"label": "Large",
"value": "6"
}]
}],
"options_available": [{
"combination": [{
"code": "color",
"value": "Red"
}, {
"code": "size",
"value": "Small"
}]
}, {
"combination": [{
"code": "color",
"value": "Red"
}, {
"code": "size",
"value": "Medium"
}]
}, {
"combination": [{
"code": "color",
"value": "Red"
}, {
"code": "size",
"value": "Large"
}]
}, {
"combination": [{
"code": "color",
"value": "Yellow"
}, {
"code": "size",
"value": "Small"
}]
}, {
"combination": [{
"code": "color",
"value": "Yellow"
}, {
"code": "size",
"value": "Medium"
}]
}, {
"combination": [{
"code": "color",
"value": "Yellow"
}, {
"code": "size",
"value": "Large"
}]
}, {
"combination": [{
"code": "color",
"value": "Pink"
}, {
"code": "size",
"value": "Small"
}]
}, {
"combination": [{
"code": "color",
"value": "Pink"
}, {
"code": "size",
"value": "Medium"
}]
}, {
"combination": [{
"code": "color",
"value": "Pink"
}, {
"code": "size",
"value": "Large"
}]
}],
"custom_options": []
}
and i have my Sampler as
import org.json.JSONObject;
import org.json.JSONArray;
String response= prev.getResponseDataAsString();
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("options");
Integer count= jsonArray.length();
vars.put('counts', count);
But while running the script I'm getting an error:
No signature of method: org.apache.jmeter.threads.JMeterVariables.put() is applicable for argument types: (java.lang.String, java.lang.Integer)
In addition to values also (Counts=2). My intention is to get the count of array in "Options" Key ( See response above)
vars.put
doesn't support different value than regular String
and you are trying to put Integer
value.
Easy way out by simple convert:
vars.put("counts", Integer.toString(count));
Another option is save object as is using vars.putObject
vars.putObject("counts", count);
Also move to JSR223 Sampler, per JMeter best practices:
Since JMeter 3.1, we advise switching from BeanShell to JSR223 Test Elements