When using Apache JMeter for API testing, we often need to extract the values of a specific field from a response in JSON format. This can be done by using JMeter's built-in JSON extractor and regular expression extractor. Here is a concrete example showing how to extract from a JSON responserowId
The value of , while processing the string terminator.
Suppose we have the following JSON response:
{ "flag": "success", "formulaStatus": -1, "encryption": "0", "changestate": 1, "href": "/pf/ovdf/bd/openPage?pr=od&ll=115dfc704f96b039825a66f15b04&rowId=D96DB6B24EEE412BB0DE7E728EE193E6", "closer": false }
Our goal is to extractrowId
Value of parameterD96DB6B24EEE412BB0DE7E728EE193E6
. This value is not presented directly as a property of the JSON object, but is nested as part of a URLhref
in the field. Therefore, we need two steps to extract this value.
Step 1: Extract the href field
First, we use the JSON extractor to capturehref
The value of the field. The JSON extractor can extract values directly from the JSON structure. We can configure the JSON extractor as follows:
- Add JSON Extractor to Request: In JMeter, select the HTTP request component, and right-click to select "Add" -> "Post Processor" -> "JSON Extractor".
- Configuring the JSON Extractor: In the configuration interface of JSON extractor, set the following parameters:
-
Variable name:
extractedHref
-
JSON path expression:
$.href
-
default value:
NOT_FOUND
After this configuration, if the JSON response is formatted correctly,extractedHref
The variable will containhref
The full URL string for the field.
Step 2: Extract rowId from URL
- Next, we need to
extractedHref
Extract from variablesrowId
value. - We can use a regular expression extractor to accomplish this:
-
Apply to variables:
extractedHref
-
Regular expressions:
rowId=([^&"]+)
-
template:
$1$
-
Match number:
1
-
default value:
NOT_FOUND
In this regular expression,rowId=([^&"]+)
It means searchingrowId=
Any sequence of characters after that, until it is encountered&
、"
or end of string. Brackets()
Represents a capture group, used to extract the matching parts. After this modification, the regular expression will encounter quotes"
stop matching, which prevents extra characters from being extracted, such as following in JSON responserowId
Quotes and other fields after the value.
in conclusion
Through the above steps, we can effectively extract from the JSON responserowId
value. This method is not only applicable to this example, but also widely used in scenarios where data needs to be extracted from nested information. The power of JMeter makes it a powerful tool for API testing and data extraction.
This is the article about using JMeter to extract specific values from the URL parameters of JSON response. For more related content on JMeter JSON extraction of specific values, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!