JSON is now a first class citizen in JMeter with the introduction of a new JSON Path post processor. This is available as an official component from JMeter 3.0

The JSON PostProcessor enables you to extract data from JSON responses using JSON-PATH syntax. This post processor is very similar to Regular Expression Extractor. It must be placed as a child of HTTP Sampler or any other sampler that has responses. It will allow you to extract in a very easy way text content, see JSON Path syntax.

In this article, I will show you what can we do with JSON Path PostProcessor.

Firstly, let see what’re the parameters and what do they mean in this component:

json_path_params
Source: JMeter Component Reference

An example of a Test Plan structure:

json_path_test_plan

  • Dummy Sampler – useful for debugging, you can put any data into “Response Data” field and make the JSON Path PostProcessor a child of the Dummy Sampler, and it will allow you to avoid sending extra requests to the server
    NOTE: You must follow this link to install this plugin before running my example test plan

  • Debug Sampler – outputs JMeter Variable values (it also can print the JMeter and System properties)
  • View Results Tree –  visualizes the Dummy and Debug samplers output

And this is JSON which will be used for all examples

The demo file json-path-postprocessor.jmx for all examples below is available here.

Extract Single Value

  • Variable names: FIRST_AUTHOR
  • JSON Path expressions: $.store.book[0].author

You will get the FIRST_AUTHOR variable with value Nigel Rees

FIRST_AUTHOR=Nigel Rees
FIRST_AUTHOR_matchNr=1

json_path_first_author

Handle Multi-Match Number

  • Variable names: AUTHOR
  • JSON Path expressions: $.store.book[*].author
  • Match Numbers: -1

Where [*] mean all the value in book array

or

  • Variable names: AUTHOR
  • JSON Path expressions: $..author
  • Match Numbers: -1

Where  ..  is recursive descent. JSONPath borrows this syntax from E4X.

It will extractor and generate all variables as below:

AUTHOR_1=Nigel Rees
AUTHOR_2=Evelyn Waugh
AUTHOR_3=Herman Melville
AUTHOR_4=J. R. R. Tolkien
AUTHOR_matchNr=4

At this time, if you want to get the first author Nigel Rees, just use ${AUTHOR_1}. Or use the ${AUTHOR_2} to get Evelyn Waugh, etc.

In case the Match Numbers: 0 –> it will extract random a value: Nigel Rees, Evelyn Waugh, Herman Melville or J. R. R. Tolkien

One more option, you can extract exactly the value which you want by using Match Numbers

Example: keep the same Variable names and JSON Path expressions above

Match Numbers: 1 –> AUTHOR=Nigel Rees

Match Numbers: 2 –> AUTHOR=Evelyn Waugh

Match Numbers: 4 –> AUTHOR=J. R. R. Tolkien

Match Numbers: 5 (greater than number of matches) –> AUTHOR= (nothing)

Extract with a Condition

Sometimes, we need to get a value which depends on the other value, i.e. get PRICE where TITLE is “The Lord of the Rings”. It’s also able to to do with JSON Path Postprocessor

  • Variable names: PRICE
  • JSON Path expressions: $..[?(@.title == ‘The Lord of the Rings’)].price

Where
[ ] – an array.
?( ) – applies a filter (script) expression.
@.title == ‘The Lord of the Rings’ – the current object which has title as child with value ‘The Lord of the Rings’

Let see what will we have:

json_path_price_with_condition.png

For more convenience, we can also parameterize the title, it may be defined before or just extracted from the other request. The expression would be
$..[?(@.title == ‘${TITLE}‘)].price

Extract Multiple values with one PostProcessor

This is the new feature for PostProcessor, no need to use multiple post processors anymore. Now, let try to extract the color and price of the bicycle at the same time. Very easy, just separate Variable Names, JSON PathExpressionsDefault Values and Match Numbers by a semi-colon ;

Variable names: BIKE_COLOR;BIKE_PRICE
JSON Path expressions: $..bicycle.color;$..bicycle.price
Match Numbers: 1;1
Default Values: NOT_FOUND;NOT_FOUND

And here are the variables we have:

BIKE_COLOR=red
BIKE_COLOR_matchNr=1
BIKE_PRICE=19.95
BIKE_PRICE_matchNr=1

json_path_multiple.png

If you want to extract more, just add more variable as syntax above.

NOTES: You should to specific all options, value and must match the number of variables. For the example above, if we just put NOT_FOUND or NOT_FOUND; for Default Values, it will cause an issue like
java.lang.ArrayIndexOutOfBoundsException: 1

This feature looks so awesome, right? However, in my perspective, I don’t prefer this way because it’s a little bit hard to see and manage many things between the variables.

Compute concatenation var

If many results are found, plugin will concat them using ‘,’ separator and store it a var named _ALL

Go back the second example in this article, the only difference is the Compute concatenation var checkbox is checked.

AUTHOR_ALL=Nigel Rees,Evelyn Waugh,Herman Melville,J. R. R. Tolkien

json_path_extractor_concat.png


Hope this article help you can understand how to work with JSON Path in JMeter. This is my very the first post, so it may contain many errors or the content is not so good enough, I also use some content from BlazeMeter, hope you all can skip it. And if you’re facing any problem while executing these samples above, or the issue just related to JSON Path in JMeter, please feel free to discuss as a comment below. Any suggestion and comment for improvement, please also leave it below.

I uploaded the json-path-postprocessor.jmx file with contains all examples above. It will useful for you to practice by yourself.

In next article, I will do the comparison between JSON Path PostProcessor and JSON Path Extractor (plugin) to see which one is better.

Advertisements