SoFunction
Updated on 2025-03-04

Use JMeter to extract specific values ​​from URL parameters of JSON response

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 responserowIdThe 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 extractrowIdValue of parameterD96DB6B24EEE412BB0DE7E728EE193E6. This value is not presented directly as a property of the JSON object, but is nested as part of a URLhrefin the field. Therefore, we need two steps to extract this value.

Step 1: Extract the href field

First, we use the JSON extractor to capturehrefThe 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 nameextractedHref
  • JSON path expression$.href
  • default valueNOT_FOUND

After this configuration, if the JSON response is formatted correctly,extractedHrefThe variable will containhrefThe full URL string for the field.

Step 2: Extract rowId from URL

  • Next, we need toextractedHrefExtract from variablesrowIdvalue.
  • We can use a regular expression extractor to accomplish this:
  • Apply to variablesextractedHref
  • Regular expressionsrowId=([^&"]+)
  • template$1$
  • Match number1
  • default valueNOT_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 responserowIdQuotes and other fields after the value.

in conclusion

Through the above steps, we can effectively extract from the JSON responserowIdvalue. 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!