Reputation: 31
Jmeter: How to extract & store the accountID in a variable from response using BeanShell preprocessor ? Can we use JSON or regular expression for this.
My Response some what look like:
{
"address":{
"street1":"6550 Vallejo Street",
"street2":"",
"city":"Emeryville",
"state":"CA",
"zipcode":"98456",
"country":"Uk"
},
"timezone":"US/Pacific-New",
"reference":"fe_qa001",
"token":"ns7h4rqVZegSZG6yZls6",
"login":"[email protected]",
"accountId":-9223372036762408565,
"firstName":"qa029",
"lastName":"svcqa",
"email":"[email protected]",
"component":[
{
"type":1,
"reference":"12-1",
"version":"1",
"base":"Plastic",
"zipcode":"94556",
"bedsize":"high",
"bed":false,
"componentId":0,
"code":"P5",
"sku":"abcd",
"serial":"1234",
"purchaseDate":1372723200000,
"chamber":2
}
]
}
I am a newbie to Jmeter. So can anyone help me here. The requirement is like exacting the account ID from the response given below and store it in a variable using BeanShell Preprocessor.
Upvotes: 3
Views: 14185
Reputation: 168157
I don't know where did you get this code, but it looks like it is using minimal-json library and uses it in a weird way, so
Also your code doesn't seem to work properly with the minimal-json-0.9.4.jar, you should modify it as:
import com.eclipsesource.json.JsonObject;
String jsonString = prev.getResponseDataAsString();
JsonObject accountId = JsonObject.readFrom(jsonString);
vars.put("accountId_BSH",String.valueOf(accountId.getLong("accountId",0L)));
You can do the same easier using JSR223 PostProcessor and Groovy language. Groovy has built-in JSON support so you won't have to add any .jars. Reference Groovy code:
import groovy.json.JsonSlurper
def jsonSlurper = new JsonSlurper();
def response = jsonSlurper.parseText(prev.getResponseDataAsString());
vars.put("accountId_BSH", response.accountId.toString());
And probably the best way is using JSON Path PostProcessor available since JMeter 3.0. The relevant configuration will be something like:
accountId_BSH
$.accountId
Upvotes: 6