Popular Posts

Sunday, May 25, 2014

Handling JSON responses in Apache JMeter

There are various types of post processor elements that we can use out of the box when handling responses in a JMeter test plan. For example, Regular Expression Extractor can be used to capture a specific value or set of values from a XML, HTML or JSON response.
However, handling JSON responses through the default regular expression extractor will be a daunting task when the JSON response becomes complex. When handling complex XML responses, Xpath extractor is the obvious choice in JMeter. Similarly, it will be quite efficient to have a similar post processor element to handle complex JSON responses.
JSONPath is a way to extract parts of a given JSON document and is now available in many programming languages. In this simple post, we will look at how we can use JSONPath expressions to extract values from JSON responses in JMeter.

Pre-Requisites:

Download and install JMeter version 2.8 or later

Step 1

Download this JMeter plugin library and unzip JMeterPlugins-ExtrasLibs-1.1.3.zip
Copy the content under lib/ directory to JMETER_HOME/lib (this will merge with the existing content)

Step 2

Restart JMeter and start to create a new test plan. Add a new thread group. Then add a HTTP Request sampler. Enter the following values.
Server name : ip.jsontest.com
Method : GET

Leave the other attribute values as they are.

Step 3

Right-click on the above HTTP Request Sampler and select Add --> Post Processors --> jp@gc - JSON Path Extractor
Enter the following values.

Variable name: ip_address (this is just a reference)
JSON Path: $.ip
Default value: NO_VALUE
















Step 4

Add a Debug sampler so that we can check the extracted value. You can add Debug sampler by right clicking on the thread group and selecting Add --> Sampler --> Debug Sampler

Step 5


Add View Results Tree listener and save the test plan. Then run the test. You will see the following response.

{"ip": "50.151.197.63"}

Step 6

The above simple JSONPath expression ($.ip) should return the ip address value from the JSON response. Check the Debug sampler output. It will look similar to the following.

JMeterVariables:
JMeterThread.last_sample_ok=true
JMeterThread.pack=org.apache.jmeter.threads.SamplePackage@61191c78
START.HMS=222538
START.MS=1401036938959
START.YMD=20140525
TESTSTART.MS=1401039015874
ip_address=50.151.197.63

The ip address value was extracted from theSON response and assigned to ip_address variable which we have specified in the JSON Path Extractor definition.

We can now use this variable down in our JMeter test plan to use in various other samplers.