Páginas

SyntaxHighlighter

quinta-feira, 31 de março de 2011

JMeter - Processing and handling JSON responses using BSF post processors

In one of my Jmeter posts I talked about how to post JSON data using a HttpSampler.
Now I want to show some cool stuff we can do using Jmeter's javascript/BSF support for handling JSON responses.

Suppose we have the following JSON response:

{ success: true, data:[ 1,2,3,4,5 ]  }

This string can be easily converted to a javascript object by using the eval function like this:

eval( 'var myObj = ' + prev.getResponseDataAsString() )

After the evaluation we can use the variable "myObj" as we wish and do things like accessing the success property:

log.info(myObj.success)

Or iterate over the list in the data property:

for (  var i = 0;  i < myObj.data.length; i++ ) {
     log.info(myObj.data[i])
}

We can also put the object in the implicit vars object so it can be used in another sampler, for example:

vars.putObject('myObj', myObj)