Extract JSON content efficiently with JMeter using JSON PATH syntax with UBIK-LOAD-PACK

0saves

Context:

You use JMeter and want to test an application that massively uses JSON data as exchange format.

JSON has become a de-facto standard nowadays and is used often:

  • To return data through Ajax Calls within dynamic websites
  • To return data through HTTP calls for Mobile Applications
  • As a REST webservice exchange format
  • As a storage format for NO-SQL databases like MongoDB

Of course you can still extract content with core JMeter using Regular Expression Extractor but this becomes quite tedious, inefficient if not impossible when you have a lot of JSON elements with same attribute names.

You can also use alternate approaches which you will find googling a little:

  • BSF or JSR223 Post Processor using Javascript which will transform response to Javascript variable and process it => DRAWBACK : Performance of JMeter are highly impacted
  • BeanShell and a JSON library => DRAWBACK : Lot of Beanshell code to write and impact on JMeter performances

This is where UBIK JSON Plugin comes to the rescue.

Description:

Our plugin uses the JSON Path syntax as described in this article:

 

  • http://goessner.net/articles/JsonPath/

 

This syntax enables very easy extraction for JSON format as we will see in following example

Example:

Suppose we have a GET HTTP request that returns the following content:

{ "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},
{ "category": "fiction","author": "Herman Melville","title": "Moby Dick","isbn": "0-553-21311-3","price": 8.99},
{ "category": "fiction","author": "J. R. R. Tolkien","title": "The Lord of the Rings","isbn": "0-395-19395-8","price": 22.99}],
"bicycle": {"color": "red","price": 19.95}} }

And we want to extract:

  • author of the first book
  • title of last book
  • all authors and iterate

How-To:

To do that we will first create the HTTP Sample that returns the JSON response and use the UBIK LOAD PACK JSON TESTER view in View Results Tree:

ULP_JSON_TESTER

 

As you can see this view:

  • Formats the JSON response in a more readable format
  • Adds a JSON Path Expression tester which helps you test and debug your JSON path expressions

Once we have found the correct syntax we can add the ULP_JSON PostProcessor to our sampler and extract our required data:

ULP_JSON PostProcessor

This will:

  1. Extract data from JSON response
  2. Store in firstAuthor variable the extraction result if found
  3. If not found, firstAuthor will contain NV_firstAuthor
  4. We can the use ${firstAuthor} in following elements to inject the data

Let’s see what happens by running the test:

ULP_JSON_RUN_RESULT

As you can see, firstAuthor variable contains the correct data.

Let’s now also extract title of book.

To improve performance and reduce code, Post-Processor allows extracting many variables in one shot, ULP_JSON PostProcessor changes to:

ULP_JSON PostProcessor2

Let’s see what happens by running the test:

ULP_JSON PostProcessor-Run2

As you can see, we now have 2 variables:

  • firstAuthor
  • firstTitle

Extracting last author would be as simple as setting JSON Path Expression to:

  • $..book[-1:].author

Just imagine doing this with regular expressions :-)

And finally we can extract all authors through:

  • $..book[*].author

And use a ForEach Controller to iterate over these:

ULP_JSON_1

ULP_JSON_2

 

ULP_JSON_3

Running this plan gives us:

ULP_JSON_RUN3

 

As you can see, the ULP_JSON PostProcessor has automatically created the necessary variables for each result found using the same behaviour as Core Regular Expression Extractor, so we have in JMeter Variables context:

  • authors_1=Nigel Rees
  • authors_2=Evelyn Waugh
  • authors_3=Herman Melville
  • authors_4=J. R. R. Tolkien

Thus we can use ForEach Controller to iterate on these variables.

Conclusion:

With our plugin your JSON based testing becomes:

  • easier to script
  • faster
  • more maintainable
  • more productive

All these advantages

  • without Performance compromise as our implementation is one of the fastest of the market
  • with a seamless integration with Core JMeter, so you can reuse all your JMeter Skills and learn only some extras related to our plugin

So what are you waiting for … :-)

More information:

Ubik Load Pack

Author:

Philippe Mouawad

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.