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":"ptrm@company.com",
: : : "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?
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:
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
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.
Another possible problem could be regarding your imports, just in case
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)
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