Dmitri Tikhanski is a Contributing Writer to the BlazeMeter blog.

Become a JMeter and Continuous Testing Pro

Start Learning
Slack

Test Your Website Performance NOW! |

arrowPlease enter a URL with http(s)
Dec 10 2020

The JMeter JSON JMESPath Extractor and Assertion: A Guide

 

From time to time we all have to deal with JSON content in JMeter tests, especially when it comes to REST API testing. The “interesting” part of the response might have to be extracted for re-using in the following request (aka correlation) or to ensure that the response contains the expected value using an assertion. JMeter 5.2 has a JMESPath extractor and assertion, which provide more features and robustness. This blog post will explain how and when to use JMESPath and what you can do with it in your performance and load tests.

JMeter JSON Test Elements: A Short History

Initially, JMeter didn’t have any test elements for working with JSON data. Then the JSON Plugin was developed, allowing us to use JSON Path language for data extraction and assertions. In JMeter 3.0, the JSON PostProcessor was added so installing 3rd-party plugins for working with JSON wasn’t required anymore. 

 

However this JSON Path approach had (and still has) some disadvantages:

 

  1. The JSON Plugin and JSON Post Processor were using different underlying libraries under the hood. This meant the syntax was different, especially when it came to “edge cases” 
  2. The JSON Path language itself doesn’t have any specifications. Therefore, implementing functionalities is up to the developer and there is no guarantee of any integrity of backwards compatibility. A function which is present in one implementation might be missing in another and vice versa.

The JMESPath Advantages and Benefits

JMESPath in turn has the complete specification describing all the syntax, functions, operators, etc. Moreover there is a TCK (Technology Compatibility Kit) which is a set of tests checking particular libraries for the specification compliance. So, there is a guarantee that a particular implementation corresponds to the specification. There should be no “surprises” and the JMESPath queries will be more robust and reliable compared to the JSONPath counterparts. 

 

As a “bonus” JMESPath is more advanced and provides more functions giving you extra flexibility and enabling certain scenarios which were not possible with the JSONPath test elements. 

 

JSON JMESPath Elements

JMeter 5.2 has introduced two new test elements:

 

  • JSON JMESPath Extractor - for fetching data from JSON responses using JMESPath query language
  • JSON JMESPath Assertion - for conditionally failing samplers if the defined criteria are not met 

 

Now let’s see how they work.

Using the JSON JMESPath

Pre-Requisites and Demo

 

All the exercises in this article assume the following JSON test structure:

 

{

 "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

   }

 },

 "expensive": 10

}

 

If you want to replicate any of the examples, the easiest way is going for Dummy Sampler and putting the JSON payload from above as the Response Data:

 

 

You can see the response data and also execute arbitrary JMESPath queries against it by using the View Results Tree listener. It provides a JSON JMESPath Tester view:

 

 

In the image above you can see how easy you can extract the “color” attribute value of the “bicycle” object in the “store”. 

 

JSON JMESPath Extractor

 

So in order to have the “red” as a JMeter Variable with the name of i.e. ${color}

 

  1. Add a JSON JMESPath Extractor as a child of the request which returns the sample JSON. Add -> Post-Processors -> JSON JMESPath Extractor
  2. Configure it as follows:
    • Names of created variables: color
    • JMESPath Expressions: store.bicycle.color
    • Provide “Match No”. As there is only 1 match go for 1. However 0 can be used as well

 

 

The extracted value can be visualized using the Debug Sampler.

 


 

JSON JMESPath Cookbook

 

A few potentially useful queries and use cases:

 

Get authors of books with a price higher than $10

 

store.book[?price > `10`].author

 

 

Going forward I’ll be providing the queries and results as text to save bandwidth, money and trees. 

 

Get more than one attribute in a single shot

 

It is also possible to get more than one attribute, for example if you want not only the author but the category as well:

 

store.book[?price > `10`].[author,category]

 

Returns:

 

Result[0]=["Evelyn Waugh","fiction"]

Result[1]=["J. R. R. Tolkien","fiction"] 

 

Get the author of “Moby Dick”

 

store.book[?title == 'Moby Dick'].author

 

Returns:

 

Result[0]=Herman Melville


 

Get authors of all books:

 

store.book[*].author

 

Returns:

 

Result[0]=Nigel Rees

Result[1]=Evelyn Waugh

Result[2]=Herman Melville

Result[3]=J. R. R. Tolkien

 

Get author of the first book

 

store.book[0].author

 

Returns:

 

Result[0]=Nigel Rees

 

Get author of the last book

 

store.book[-1].author

 

Returns:

Result[0]=J. R. R. Tolkien

 

Query attributes names


 

With JMESPath you can also get attribute names with “keys” function like:

 

keys(store.bicycle)

 

Returns:

 

Result[0]=color

Result[1]=price


 

keys(@) 

 

Returns:

 

Result[0]=store

Result[1]=expensive


 

Pro tip: In JMESPath the @ symbol stands for the current node.

 

JSON Path implementations don’t have an equivalent function as of now. Prior to the JMESPath Extractor you had to use JSR223 Test Elements and Groovy language for attribute validation. 

 

So, given this simple JSON object:

 

{

   "hello" : "world"

}

 

With a JSONPath you could only get this "world" value and there was no way to get "hello". With JMESPath it's possible to get both.

JSON JMESPath Assertion

 

For Assertions use the same principle as above:

 

  1. Provide the JMESPath query - if the query will not match anything, the Sampler will be marked as failed.
  2. Provide the JMESPath query and the expected result - the Assertion will compare the output of the query with the expected result and fail in case of mismatch.

 

See below what happens when we change the expected value from “red”, which appears in the output, to “green”, which does not.

 



 

So if you will be in the need of extracting something from JSON responses or coming up with an assertion - consider using JSON JMESPath test elements. They don’t have any performance overhead and at the same time they provide more features and tend to be more future-proof than the existing JSONPath-based solutions.

Scale with BlazeMeter

Once you’ve finished building and running your JMeter test, scale your test with BlazeMeter. Test for more locations and higher volumes of traffic. Get advanced reporting, CI/CD integrations and an easier to use UI. Start now.


 

   
arrowPlease enter a URL with http(s)

Interested in writing for our Blog?Send us a pitch!