If you’re here, it’s probably because you need to extract variables from a Json response using JMeter.
Good news! You’re on the definitive guide to master JMeter Json Extractor. Complementary to our Rest API Testing Guide, you’ll learn everything you need to master Json Path Expressions.
Let’s go! And don’t panic, there is nothing difficult down there.
Json format
To get a better understanding of what Json is, here is an example Json document:
Json path syntax
1 |
JSON Path |
Description |
2 |
$ |
The root object/element, all JSON Path usually start with this sign. |
3 |
. |
Child operator, use this dot to move from parent to child. |
4 |
[] |
Using these braces, include with the index to access the elements of an array. Note: index of array start from 0 |
5 |
* |
Wildcard. All objects/elements regardless their names. |
6 |
.. |
Recursive descent |
7 |
@ |
Indicate the current object, element |
8 |
?() |
Apply a filter (script) expression |
Now, I will show with you guys some examples:
1.
Following the above Json example, I want to get name is “Zack” and age is 30 :
So, my Json path: $.name and $.age
I want to get Value of hobbies. So:
- $.hobbies[0] will return value “music”
- $.hobbies[1] will return value “football”
- $.hobbies[2] will return value “movies”
I have another example like that:
JSON Path |
Description |
$.store.* |
All things, both books and bicycles |
JSON Path |
Description |
$.store..price |
The price of everything |
JSON Path |
Description |
$..book[-2:] |
The second to last book |
JSON Path |
Description |
$..book[0,1] |
The first two books |
JSON Path |
Description |
$..book[:2] |
All books from index 0 (inclusive) until index 2 (exclusive) |
JSON Path |
Description |
$..book[1:2] |
All books from index 1 (inclusive) until index 2 (exclusive) |
JSON Path |
Description |
$..book[2:] |
Book number two from tail |
JSON Path |
Description |
$..book[?(@.isbn)] |
All books with an ISBN number |
JSON Path |
Description |
$..book[?(@.price<10)] |
All books in store cheaper than 10 |
JSON Path |
Description |
$..book[?(@.price <= $[‘expensive’])] |
All books in store that are not ”expensive” |
JSON Path |
Description |
$..book[?(@.author =~ /.*REES/i)] |
All books matching regex (ignore case) |
JSON Path |
Description |
$..* |
Give me every thing |
JSON Path |
Description |
$..book.length() |
$..book.length() |