The JMeter JSON Extractor is a robust post-processor designed for retrieving specific data points from JSON-formatted responses using JSONPath expressions. Similar to other extractors like the Regular Expression Extractor, it must be added as a child element to an HTTP Sampler or any other sampler that returns JSON data.
JSON Extractor Configuration Parameters
Name: This field specifies the unique identifier for the extractor in the test plan tree.
Apply to: This setting determines which part of the sampler's response the extractor should process, especially relevant for samplers that might generate sub-samples (e.g., HTTP requests with embedded resources).
- Main sample only: Processes only the primary sampler's response.
- Sub-samples only: Processes only responses from child samples generated by the main sampler.
- Main sample and sub-samples: Applies to both the main sampler and its generated sub-samples.
- JMeter Variable Name to use: Specifies a JMeter variable whose content will be processed for JSON extraction.
Names of created variables: A semicolon-separated list of variable names. Each name will store the result of its corresponding JSONPath expression. The number of variable names must match the number of JSONPath expressions.
JSON Path Expressions: A semicolon-separated list of JSONPath expressions used to query the JSON response. The number of expressions must match the number of variable names.
Default Values: A semicolon-separated list of default values. If a JSONPath expression fails to find a match, its corresponding variable will be assigned this default value. The count of default values should align with the number of variables.
Match Numbers: Controls which matched value is extracted when a JSONPath expression yields multiple results. This is a semicolon-separated list, aligning with the JSONPath expressions.
- 0 (Random): Extracts a random match from the found results (default behavior if unspecified).
- -1 (All): Extracts all matching values. These values are stored in sequentially indexed variables:
<variable name>_1,<variable name>_2, ..., up to<variable name>_N(where N is the total number of matches). - X (Specific): Extracts the X-th match (1-based index). If the X-th match does not exist, the variable remains unset, or the default value is applied.
Regardless of match success, the total count of matches for each expression is stored in a special variable: <variable name>_matchNr. If no matches are found, this variable will be set to 0.
Compute concatenation var: If checked, and a JSONPath expression returns multiple matches, all matched values will be concatenated into a single string (separated by commas) and stored in an additional variable named <variable name>_ALL.
Practical Example of JSON Extraction
Consider an authentication endpoint that returns a JSON response like the following after a successful login:
{
"sessionToken": "a1b2c3d4e5f67890abcdef1234567890",
"userAccount": "testuser",
"allowedEnvironments": [
{
"envId": "DEV001",
"envName": "Development Environment",
"accessLevels": ["READ", "WRITE"]
},
{
"envId": "PROD001",
"envName": "Production Environment",
"accessLevels": ["READ"]
}
],
"statusMessage": "Authentication successful"
}
To extract the sessionToken and all envId values, the JSON Extractor can be configured as follows:
- Names of created variables:
authToken;environmentId - JSON Path Expressions:
$.sessionToken;$.allowedEnvironments[*].envId - Match Numbers:
; -1(no specific match forauthToken, all forenvironmentId)
After executing the request, a Debug PostProcessor might show the following JMeter variables:
JMeterVariables:
authToken=a1b2c3d4e5f67890abcdef1234567890
authToken_matchNr=1
environmentId_1=DEV001
environmentId_2=PROD001
environmentId_matchNr=2
Fundamentals of JSONPath Expressions
JSONPath expressions allow traversing JSON structures using either dot notation ($.data.field) or bracket notation ($['data']['field']).
Core Operators
| Operator | Description |
|---|---|
$ |
The root element of the JSON structure. All expressions begin here. |
@ |
Represents the current node being processed within a filter predicate. |
* |
Wildcard. Matches any member name or array index. |
.. |
Deep scan. Searches for the specifeid element recursively at any level. |
.<name> |
Selects a child node with the given name. |
['<name>' (, '<name>')] |
Bracket notation for child or descendant nodes, useful for names with special characters or spaces. |
[<number> (, <number>)] |
Selects a single or multiple array elements by index. |
[start:end] |
Array slice operator (exclusive of end index). |
[?(<expression>)] |
Filter expression that must evaluate to a boolean. |
Available Functions
Functions can be appended to a path expression, operating on the results of the path. The output of the path becomes the input for the function.
| Function | Description | Output Type |
|---|---|---|
min() |
Computes the minimum value from a numeric array. | Double |
max() |
Computes the maximum value from a numeric array. | Double |
avg() |
Calculates the average of values in a numeric array. | Double |
stddev() |
Determines the standard deviation of a numeric array. | Double |
length() |
Returns the count of elements in an array or string length. | Integer |
sum() |
Sums all values in a numeric array. | Double |
append(X) |
Adds element X to the end of the input array. |
Same as input |
Filter Operators
Filters are boolean expressions used within [?(<expression>)] to select elements from an array. The @ symbol refers to the current item being evaluated. Complex filters can be constructed using && (AND) and || (OR) logical operators. String literals must be enclosed in single or double quotes.
| Operator | Description |
|---|---|
== |
Equality check. |
!= |
Inequality check. |
< |
Less than. |
<= |
Less than or equal to. |
> |
Greater than. |
>= |
Greater than or equal to. |
=~ |
Matches against a regular expression (e.g., [?(@.name =~ /pattern/i)]). |
in |
Checks if a value is present in a list (e.g., [?(@.size in ['S', 'M'])]). |
nin |
Checks if a value is NOT present in a list. |
subsetof |
Checks if the left-side array is a subset of the right-side array. |
anyof |
Checks for any intersection between the left and right arrays. |
noneof |
Checks for no intersection between the left and right arrays. |
size |
Compares the length of an array or string to a value. |
empty |
Checks if an array or string is empty. |
Illustrative JSONPath Examples
Consider the following JSON structure representing a product catalog:
{
"catalog": {
"products": [
{
"id": "P001",
"name": "Laptop Pro",
"category": "Electronics",
"price": 1200.00,
"inStock": true,
"tags": ["laptop", "high-end"]
},
{
"id": "P002",
"name": "Mechanical Keyboard",
"category": "Accessories",
"price": 150.00,
"inStock": false,
"tags": ["keyboard", "gaming"]
},
{
"id": "P003",
"name": "Wireless Mouse",
"category": "Accessories",
"price": 75.50,
"inStock": true
},
{
"id": "P004",
"name": "External Monitor",
"category": "Electronics",
"price": 300.00,
"inStock": true,
"tags": ["monitor"]
}
],
"discounts": {
"electronicsDiscount": 0.10,
"accessoriesDiscount": 0.05
}
},
"featuredProduct": "P001"
}
| JSONPath Expression | Description | Example Result (Simplified) |
|---|---|---|
$.catalog.products[*].name |
Names of all products. | ["Laptop Pro", "Mechanical Keyboard", "Wireless Mouse", "External Monitor"] |
$..price |
Prices of all products. | [1200.0, 150.0, 75.5, 300.0] |
$.catalog.products[1] |
The second product in the list. | {id: "P002", ...} |
$..products[-1] |
The last product. | {id: "P004", ...} |
$.catalog.products[0,3] |
The first and fourth products. | [{id: "P001", ...}, {id: "P004", ...}] |
$.catalog.products[:2] |
The first two products (indices 0 and 1). | [{id: "P001", ...}, {id: "P002", ...}] |
$..products[?(@.inStock == true)] |
All products currently in stock. | [{id: "P001", ...}, {id: "P003", ...}, {id: "P004", ...}] |
$.catalog.products[?(@.price < 100)] |
Products with a price less than 100. | [{id: "P003", ...}] |
$..products[?(@.category == 'Electronics' && @.price > 1000)] |
Electronics products priced over 1000. | [{id: "P001", ...}] |
$..products[?(@.name =~ /mouse/i)] |
Products with 'mouse' in their name (case-insensitive). | [{id: "P003", ...}] |
$.catalog.products.length() |
Total number of products. | 4 |
$.catalog.products[?(@.tags && @.tags.length() > 1)] |
Products with more than one tag. | [{id: "P001", ...}, {id: "P002", ...}] |
$.catalog.products[?('gaming' in @.tags)] |
Products tagged as 'gaming'. | [{id: "P002", ...}] |
$.catalog.products[?(@.price * (1 - $.catalog.discounts.electronicsDiscount) < 1000)] |
Electronics products that would be under 1000 after discount. | [{id: "P004", ...}] |