The most surprising thing about JMeter’s JSON Extractor is that it doesn’t actually parse JSON; it uses a regular expression engine to find patterns within the JSON string.
Let’s see it in action. Imagine you’ve just made an API call that returns a JSON response like this:
{
"status": "success",
"data": {
"userId": "user123",
"sessionToken": "abc123xyz789"
},
"timestamp": "2023-10-27T10:00:00Z"
}
You want to extract the sessionToken to use in a subsequent request. In JMeter, you’d add a JSON Extractor post-processor to the HTTP Request sampler that generated this response.
Here’s how you’d configure it:
- Name:
Extract Session Token - Apply to:
Main sample only(orAll active sub-samplesif your request has sub-samples you want to process) - Field to be extracted:
Body(This tells JMeter to look in the response body) - JSON Path expressions:
$.data.sessionToken - Reference name:
session_token - Match No.:
0(This means "get the first match") - Default Value:
NOT_FOUND
After this extractor runs, JMeter will create a JMeter variable named session_token and its value will be "abc123xyz789". If the sessionToken wasn’t found, the variable session_token would be set to NOT_FOUND. You can then use this variable in a subsequent HTTP Request sampler like this:
http://api.example.com/data?token=${session_token}
This mechanism allows you to dynamically chain API calls, using data from one response to inform the next. It’s fundamental for testing any API that returns structured data and expects that data to be passed back in subsequent requests.
The JSON Extractor relies on the JSONPath syntax, which is a query language for JSON. The $ represents the root of the JSON object. So, $.data.sessionToken means "start at the root, go into the data object, and then get the value of the sessionToken key." You can also use wildcards, array indexing, and even filter expressions within your JSONPath. For example, to get all session tokens if there were multiple, you might use $.data[*].sessionToken.
The "JSON Path expressions" field accepts multiple expressions. If you provide more than one, JMeter will try to extract values for each. The "Reference name" field is what you’ll use to refer to the extracted value. If you have multiple expressions, you can use numbered reference names (e.g., ref_name_1, ref_name_2) or specify a single reference name which will be used for all extracted values, with JMeter appending an index if multiple matches are found for a single expression.
One subtle but powerful aspect is how the "Match No." field works. While 0 is common for "first match," you can specify a positive integer to get the Nth match. For instance, 2 would retrieve the second occurrence of the pattern. If you want to capture all occurrences of a pattern, you can set "Match No." to 0 and then use JMeter’s __V() function or loop controllers to iterate through the generated variables. JMeter will create variables like reference_name_1, reference_name_2, etc., for each match found. This is incredibly useful when an API might return a list of items, and you need to process each one.
The "Default Value" is crucial for error handling and conditional logic. If JMeter cannot find a match for your JSONPath expression, the reference variable will be set to this default value. This allows you to use a If Controller to check if a value was extracted successfully before proceeding with subsequent steps, preventing downstream errors.
Beyond simple key-value extraction, JSONPath supports filtering and array manipulation. For instance, if your JSON looked like this:
{
"users": [
{"id": 1, "name": "Alice", "active": true},
{"id": 2, "name": "Bob", "active": false},
{"id": 3, "name": "Charlie", "active": true}
]
}
You could extract the names of all active users with the JSONPath expression $.users[?(@.active==true)].name. JMeter would then create variables like active_user_names_1 (Alice) and active_user_names_2 (Charlie).
The next concept you’ll likely encounter is handling different content types and encoding issues within API responses, which can sometimes interfere with the JSON Extractor’s ability to find patterns.