Test environment
JMeter 5.4.1
Plug-in introduction
The JSON PostProcessor allows you to extract data from JSON-formatted responses using the JSON Path syntax. Similar to the regular expression extractor, it must be located under the HTTP sampler or other sampler that can return JSON data as a child node.
Plug-in parameters
Name
The name that appears in the script tree structure
Apply to:
This is used for samplers that can generate subsamplers, such as HTTP samplers carrying nested resources, mail readers, or sampling controlled by a transaction controller.
-
Main sample only
Only available for main sample
-
Sub-samples only
Only available for subsampling
-
Main sample and sub-samples
Applies to main sampling and subsampling
-
JMeter Variable Name to use
Extract the contents applicable to the named variable.
Names of created variables
Variable names separated by English semicolons; These variables are used to store the results extracted from the corresponding JSON-PATH expressions (must match the number of JSON-PATH expressions).
JSON Path Expressions
JSON-PATH expression separated by semicolon ; (must match the number of variables)
Default Values
If the JSON-PATH expression of the corresponding variable does not return any results, the default value of the corresponding variable is separated by an English semicolon; (must match the number of variables)
Match Numbers
For each JSON-PATH expression, if the expression query yields multiple results, you can choose to extract that value as the variable value.
- 0 means random (default for matching numbers)
- -1 Extract all results, which will be stored in a variable named
(N takes a value from 1 to the number of extracted results)_N - X means extracting the Xth result. If the Xth result does not exist, no value will be returned and the corresponding default value will be used directly as the variable.
These matching numbers must be separated by English semicolons and match the number of JSON-PATH expressions. If no matching number is configured, 0 is used by default as the default value for each expression.
Note: Regardless of whether the corresponding JSON-PATH expression can query the results, the program will store the number of results (set to 0 if no results can be found) into the variable
Compute concatenation var
If checked, it means that if the corresponding expression queries multiple results, the plug-in will use , to connect these values and store them in a variable named
.
Plug-in usage example
The login request returns information similar to the following
{"token":"73ab6c33c39a46c1b27ae314b7a7eb1e","userName":"Test","warehouseList":[{"warehouseCode":"001DSC"," warehouseName":"Test warehouse","areas":[{"zonegroupCode":"A1","zonegroupdescr":"A1 area"},{"zonegroupCode\ ":"A2","zonegroupdescr":"A2 Zone"},{"zonegroupCode":"A3","zonegroupdescr":"A3 Zone"},{ "zonegroupCode":"A4","zonegroupdescr":"A4 Zone"}]}],errorMsgPrams":["SUCCESS"]}
The extracted relevant variable values observed through Debug PostProcessor are as follows
JMeterVariables: areas=[{"zonegroupCode":"A1","zonegroupdescr":"A1 area"},{"zonegroupCode":"A2","zonegroupdescr":\ "A2 Zone"},{"zonegroupCode":"A3","zonegroupdescr":"A3 Zone"},{"zonegroupCode":"A4","zonegroupdescr ":"A4 Area"}] areas_matchNr=1 token=d50350c345824a95ba8e1e4d43270fff token_matchNr=1 zonegroupCode_1=A1 zonegroupCode_2=A2 zonegroupCode_3=A3 zonegroupCode_4=A4 zonegroupCode_5=A5 zonegroupCode_matchNr=5
Introduction to JSON-PATH expression
JsonPath expressions can use dot notation
$.store.book[0].title
or bracket mark
$['store']['book'][0]['title']
Operator
Operator | Description |
---|---|
$ |
The root element to be found. All JSON PATH expressions begin with this |
@ |
The current node being processed by a filter predicate (The current node being processed by a filter predicate) |
* |
wildcard. Can represent a name or number |
.. |
Deep scan. Can represent a name |
. |
Get child nodes. |
[' |
Bracket marked Child node or descendant node |
[ |
Single or multiple Array index. |
[start:end] |
Array slicing operator. Note that end |
[?( |
filter expression is not included and must be a boolean expression. |
function
You can call a function at the end of a path expression-the output of the expression is the input to the function. Common functions are as follows
Function | Description | Output type |
---|---|---|
min() | Get the minimum value of the numeric array. | Double |
max() | Get the maximum value of the numeric array. | Double |
avg() | Get the average of the numeric array. | Double |
stddev() | Get the standard deviation of the numeric array. | Double |
length() | Get the length of the array | Integer |
sum() | Get the sum of the numeric array. | Double |
append(X) | Add an element to the JSON-PATH expression output array | Same as input |
filter operator
A filter is a logical expression used to filter an array. A typical filter is [?(@.age > 18)]
, where @
represents the current value being processed. item. More complex filters can be created using the logical operators & amp; & amp;
and ||
. String literals must be enclosed in single or double quotes, in the form ([?(@.color == 'blue')]
or [?(@.color == "blue")]
)
Operator | Description |
---|---|
== |
Equal |
!= |
Not equal |
< |
less than |
<= |
Less than or equal |
> |
Greater than |
>= |
Greater than or equal |
=~ |
Match regular expression , in the form of [?(@.name =~ /foo.*?/i)] |
in |
Contained in, in the form [?(@.size in ['S', 'M'])] |
nin |
not included in |
subsetof |
Subsets, in the form of [?(@.sizes subsetof ['S', 'M', 'L'])] |
anyof |
The value on the left side of the operator must have an intersection with the right side (left has an intersection with right), in the form of [?(@. sizes anyof ['M', 'L'])] |
noneof |
There is no intersection between the value on the left side of the operator and the right side |
size |
The length of the array or string on the left side of the operator must match The right side |
empty |
The left side of the operator must be an empty array or string |
JSON PATH example
Given the following json
{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honor", "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 }
JsonPath | Results |
---|---|
$.store.book [*].author |
represents the author of all books. |
$..author |
Indicates all authors |
$.store.* |
Everything--all books and bikes. |
$.store..price |
The price of everything |
$..book[2] |
The third book |
$..book[- 2] |
The penultimate book |
$..book[0,1] |
The first two books |
$..book[:2] |
The index is 0 to 2 (excluding 2) |
$..book[1:2] |
The index is 1 to 2 All books (excluding 2) |
$..book[-2:] |
The last two books |
$..book[2:] |
All books with index 2 and thereafter. |
$..book[?(@.isbn)] |
All books carrying an ISBN number |
$.store.book[?(@.price < 10)] |
Everything in the store with a price lower than 10 books. |
$..book[?(@.price <= $['expensive'])] |
All non-"expensive" books |
$..book[?(@.author =~ /.*REES/i)] |
All books matching regular expressions (ignoring case) |
$..* |
Return everything |
$..book.length() |
Number of books |