Overview
Apache JMeter enables the extraction of specific values from JSON responses for use in subsequent requests. This capability relies on JSON Path expressions to navigate and retrieve data from structured JSON content.
Setting Up Common Request Parameters
When working with multiple HTTP samplers, common configuration values like server IP, protocol, port, and encoding can be centralized using the HTTP Request Defaults configuration element. This appproach simplifies maintenance and environment migration.
Parameter resolution follows a priority hierarchy where values defined directly in individual HTTP requests override those specified in the defaults. For example, if port 8080 is set in defaults but 8888 is specified in a sampler, the latter takes precedence.
Implementing JSON Extractor
The JSON Extractor component utilizes JSON Path syntax to parse response data. Below are fundamental path operators:
$: Root node of the JSON document.: Direct child node navigation..: Recursive descant through all nodes
Example JSON structure:
{
"id": "1712903618659638",
"state": {
"code": 0,
"msg": "操作成功"
},
"data": {
"total": 4,
"dataList": [
{
"title": "默认分类"
}
]
}
}
Sample JSON Path expressions:
$.idretrieves "1712903618659638"$.data.dataList[0].titleaccesses the first array element's title$..titlefinds the first available title field recursively
Practical Implementation Example
Consider an API response containing course configuration details:
{
"data": {
"courseConfig": {
"businessId": 2100000881350072,
"liveId": 2000002089,
"title": "名称不符合运营规范"
}
},
"state": {
"msg": "操作成功",
"code": 0
}
}
To extract the liveId value, configure the JSON Extractor with the expression: $.data.courseConfig.liveId
Parameterizing Subsequent Requests
After extracting values, they can be referenced in downstream requests:
- Create a new HTTP request sampler configured as GET method
- In the request body section, reference extracted variables using
${variableName}syntax - The variable name corresponds to the reference name configured in the JSON Extractor
During execution, JMeter substitutes these placeholders with actual extracted values from previous responses, enabling dynamic parameter passing between requests.