How to extract data from Json response using JMeter
By Jérôme Loisel - CTO, on .

Categories: Tags: jmeter design performance

How to extract data from Json response using JMeter

What is Json

To get a better understanding of what Json is, here is an example Json document:

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

An increasing number of REST APIs and servers are using Json as their primary data exchange format. At OctoPerf, we are heavily using Json to exchange data between our AngularJS frontend client and our Spring Boot backend.

Since, JMeter 3.0, it’s far easier to extract data from Json responses using the Json variable extractor. Json is an extremely simple data format which has taken over XML a few years ago.

JMeter Json Path Extractor

JMeter’s Json Post Processor uses Json Way, a Java Json Path API, to perform JSon path extraction on server responses.

JMeter Json Post-Processor

Here are some example Json Path expressions that can be used to extract data from the Json document exposed above:

JsonPath (click link to try) Result
$.store.book[*].author The authors of all books
$..author All authors
$.store.* All things, both books and bicycles
$.store..price The price of everything
$..book[0,1] The first two books
$..book[:2] All books from index 0 (inclusive) until index 2 (exclusive)
$..book[2:] Book number two from tail
$..book[?(@.isbn)] All books with an ISBN number
$.store.book[?(@.price < 10)] All books in store cheaper than 10
$..book[?(@.price <= $[‘expensive’])] All books in store that are not “expensive”
$..book[?(@.author =~ /.*REES/i)] All books matching regex (ignore case)
$..* Give me every thing
$..book.length() The number of books

As you can see, it’s easy and flexible to query specific information from a Json document and put them into variables.

Configuring Json Path Extractor

The Json Path extractor should be placed under an HTTP Sampler. It has several possible settings, hence the most relevant are:

  • Variables Names: semi-colon separate variable names,
  • JSON Path Expressions: self explanatory.
  • Default values: in the case the expression doesn’t apply to the json document being processed.

Common pitfalls

The common mistakes that should be avoided:

  • Don’t define multiple variables within a single Json Path extractor: the script may become hard to understand / maintain,
  • Don’t write expressions susceptible to work only on specific json responses, try to stick to the general case,
  • The simpler the solution, the better will be the script maintainability.

Alternate techniques

Depending on the case, you may use alternate techniques to extract content from a server response.

Regular Expression Extractor

Suppose you have a very simple Json document with the following content and you want all first names:

{
 "name":"Simpsons family",
 "members":[
   {"firstName":"Homer", "lastName":"Simpson"},
   {"firstName":"Marge", "lastName":"Simpson"},
   {"firstName":"Bart", "lastName":"Simpson"}
  ]
}

In this case, the regular expressions extractor may fit well because it’s very simple to write a regular expression.

JMeter Regexp Post-Processor

We have defined the following settings:

  • Regular expression: “firstName”:“(.+?)”,
  • Template: $1$,
  • Match Nr: 3, (we want Bart)
  • Default value: whatever you want in case of error.

JSR223 with External Library

By using the Minimal Json Library, and adding it to JMeter you can do the job of extracting json data from a server response too.

Configuring JMeter With an external Lib

Now create a JSR223 Post processor under the Http Sampler whose server response is a Json document. Select Java language and inspire from the following script:

import com.eclipsesource.json.JsonObject;

String jsonString = prev.getResponseDataAsString(); 
JsonArray members = Json.parse(jsonString).asObject().get("members").asArray();
vars.put("firstName",String.valueOf(members.get(2).getString("firstName","")));

The code above extract the firstName of the third family member and puts it in a variable.

JSR223 with Groovy

JSR223 PostProcessor has Groovy language support which has built-in JSON support so you won’t have to add any .jars. Example code:

import groovy.json.JsonSlurper

def jsonSlurper = new JsonSlurper();
def response = jsonSlurper.parseText(prev.getResponseDataAsString());
vars.put("firstName", response.members[2].firstName.toString());

BeanShell Json Extractor

Although the same result can be achieved using the BeanShell post processor, we don’t recommend to do so for performance reason. JSR223 post processors should be used in favor of BeanShell post processors. JSR223 with Groovy is several magnitudes faster than BeanShell.

BeanShell Regexp Post-Processor

Configuration is very similar to JSR223.

JMeter Plugins (Json Path Extractor)

Since JMeter 3.0, JMeter Json Extractor Plugin should be abandoned in favor of the built in Json Path extractor. This plugin is still useful if you are using older JMeter versions. (2.13 and below)

Json Path JMeter Plugin

Use cases

Json extractors are particularly useful in the following cases:

  • Json REST Apis (trending),
  • OAuth2 authentication mechanisms, which uses Json to send and receive access and refresh tokens,
  • Single Page Web Apps (React or AngularJS are mostly seen) which communicate with JSon REST backends.

Conclusion

There are several ways to extract data from Json document using JMeter. Our favorite one is the built-in Json Extractor. And, best of all, Json extractor is fully supported by OctoPerf!

Related Content