Reputation: 195
I am trying to extract the value of one variable of a JSON array using Beanshell postprocessor but I am not getting any response in log
My JSON somewhat looks like:
"store":
: [
: : {
: : : "storeId":12345,
: : : "storeName":"ABC",
: : : "storeAddress":"DEFGHIJKL",
: : : "storeMinOrderAmount":100,
: : : "mobile":"+911234567890",
: : : "mobileSecondary":null,
: : : "city":"Somewhere",
: : : "pincode":123456,
: : : "country":"India",
: : : "email":"[email protected]",
: : : "pickup":true,
: : : "delivery":false,
: : : "storeSplashPath":null,
: : : "storeSplashType":null,
: : : "distance":"0.10"
: : },
And my Beanshell Post Processor is:
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import com.eclipsesource.json.*;
print("*******************");
//Get Store total count
int totalStoreNumber = StringUtils.countMatches(new String(data), "storeId");
print("Total Number of Stores are: " + totalStoreNumber);
if (totalStoreNumber > 0) {
//Check for Fulfilment type is "Pickup"
String jsonString = prev.getResponseDataAsString();
JsonObject store = JsonObject.readFrom(jsonString);
JsonArray store = store.get("store").asArray();
String pickup = store.get(1).asObject().get("pickup").asString();
vars.put("fulfilmentType_BSH", pickup);
print("Is Pickup allowed: " + pickup);
}
else {
print("No Stores Nearby");
}
I don't know where I am going wrong. I had read the related queries but couldn't get this right. Any Idea?
Upvotes: 5
Views: 16343
Reputation: 168122
First of all, why don't you use JSON Path PostProcessor for it? You can get absolutely the same using single simple JSON Path expression like:
$.store[0].pickup
If for any reason you still need to do it in Beanshell I have some ideas:
This is definitely the error. You cannot declare 2 variables with the same name in Beanshell script
JsonObject store = JsonObject.readFrom(jsonString);
JsonArray store = store.get("store").asArray();
// ^^^^^ ka-boom!
Possible problem. IndexOutOfBoundsException if there will be only 1 store in the response. In Beanshell collections are zero-based, 1st element will have index of 0.
String pickup = store.get(1).asObject().get("pickup").asString();
// ^ ka-boom!
Another possible problem could be regarding your imports, just in case
import org.json.JSONArray;
import org.json.JSONObject;
import com.eclipsesource.json.*;
Have you added the relevant jars to JMeter Classpath and have you restarted JMeter after this? Are you sure you're using methods correctly?
Here is your code re-implemented using json-smart which comes with JMeter 3.0 (you don't need any other jars)
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
import org.apache.commons.lang.StringUtils;
//Get Store total count
int totalStoreNumber = StringUtils.countMatches(new String(data), "storeId");
log.info("Total Number of Stores are: " + totalStoreNumber);
if (totalStoreNumber > 0) {
//Check for Fulfilment type is "Pickup"
String jsonString = new String(data);
JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE);
JSONObject store = (JSONObject) parser.parse(data);
JSONArray storeArray = (JSONArray) store.get("store");
String pickup = ((JSONObject) storeArray.get(0)).getAsString("pickup");
vars.put("fulfilmentType_BSH", pickup);
log.info("Is Pickup allowed: " + pickup);
} else {
log.info("No Stores Nearby");
}
And the evidence of its work
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using Beanshell scripting in your JMeter tests
Upvotes: 9