JMeter---JSON extractor

JMeter's JSON Extractor is an element for extracting data from JSON responses. It can extract the values ​​of specific fields from the JSON response and use these values ​​for subsequent test steps.

The steps to use the JSON extractor are as follows:

  • Add an HTTP request for fetching data with a JSON response.

  • Add a JSON extractor element after the HTTP request.

  • Configure relevant fields in the JSON extractor.

The configuration of the JSON extractor includes the following fields:

  • "name of created variables": Specify a variable name to store the extracted values.

  • "JSON Path expression": Specify a JSON expression for locating the value to be extracted. Nested fields can be accessed using a dot (.), eg "user.name".

  • "Match Numbers": Specifies the index numbers of the values ​​to be extracted. If a JSON expression returns multiple matches, the match number can be used to select one of them. (0 means random, 1 means the first, -1 means all), it can be empty, which means the default is the first.

  • "Compute concatenation var(suffix_ALL)": Whether to count all, that is, to save all matched values, named "variable name_ALL", there are multiple values ​​that need to be obtained in the usage scenario, and this group of data needs to be operated later.

  • "Default Value": The default value when no value is obtained.

When the JSON extractor is configured, it will extract the value of the specified field after each HTTP request and store it in the specified variable. These variables can be used in subsequent test steps.

When using JMeter's JSON Extractor, JSONPath syntax can be used to locate and extract specific field values ​​in JSON responses.

The following is a detailed description of the JSONPath syntax:

  • $: root node. Use $ to indicate the root node of JSON.

  • .: current node. Use . to represent the current node.

  • ..: Recursive descent, used to find matches in the current node and its children. For example, $.store..price means to find all price fields in the store node under the root node and its child nodes.

  • *: wildcard, matching any field. For example, $.store.* means to match all the fields in the store node under the root node.

  • []: Subscript operator, used to select elements in an array or attributes in an object. For example, $.store.book[0] means selecting the first element of the book array in the store node under the root node.

  • [n]: Selects the nth element in the array. For example, $.store.book[2] means to select the third element of the book array in the store node under the root node.

  • [start:end]: Select the range of elements from start to end in the array. For example, $.store.book[0:2] means to select the first three elements of the book array in the store node under the root node.

  • [?(expression)]: Filter to filter matches based on the result of the expression. For example, $.store.book[?(@.price < 10)] means to select the elements whose price is less than 10 in the book array in the store node under the root node.

    Example: Assume the following JSON response:

{
  "store": {
    "book": [
      {
        "title": "Book 1",
        "price": 10
      },
      {
        "title": "Book 2",
        "price": 15
      },
      {
        "title": "Book 3",
        "price": 5
      }
    ]
  }
}

If you want to extract the price of the second book, you can use the JSONPath expression $.store.book[1].price. This returns 15 as the extracted value.

In addition to the above basic syntax, JSONPath also supports some other advanced features, such as functions, conditional expressions, etc. These functions can be used in JSONPath according to specific needs.

Finally: The complete software testing video tutorial below has been sorted out and uploaded, and friends who need it can get it by themselves [Guaranteed 100% free]

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

Guess you like

Origin blog.csdn.net/AI_Green/article/details/132601805