JMeter – JSON Extractor

In JMeter, JSON (JavaScript Object Notation) Extractor is used to extract the values from JSON response. Actually, JSON is a simple text which is used to exchange information between the client and the server. It is written with the JavaScript object. Due to the increase, in the use of the REST APIs, the JSON is used as a primary data exchange format. You can get detailed and useful information on JSON at this site.

When you need to extract the values from JSON response and pass them into some other request in the JMeter script then you can use JSON Extractor. It is a post-processor. As I explained in my previous post, the post-processor runs after receiving the response from the server and then performs the task like extracting the dynamic value etc. JSON extractor acts in the same way. Let’s try to understand how it works.  

How to add ‘JSON Extractor’?

Follow the below steps:

  1. Select the ‘Sampler’ element whose response contains JSON value and you want to capture
  2. Right-click on the sampler
  3. Hover the cursor on ‘Add’
  4. Hover the cursor on ‘Post Processors’
  5. Click ‘JSON Extractor’

What are the input fields of ‘JSON Extractor’?

‘JSON Extractor’ has the following input fields:

  1. Name: To provide the name of the post-processor
  2. Comments: To provide arbitrary comments (if any)
  3. Apply to: To define the search scope of dynamic value.
    1. Main sample and sub-samples: In case, the request is redirected then use this search scope, so that the required JSON value can be searched in the response of both main and re-directed requests.
    2. Main sample only: When the request is not re-directed or JSON value is present only in the response of the main request then use this search scope.
    3. Sub-samples only: When the request is re-directed and the required JSON value is available in the response of the re-directed request then you can use this search scope.
    4. JMeter Variable Name to use: If JSON value needs to be extracted from the value of any JMeter variable then you need to select this option and provide the JMeter Variable name in the text field.
  4. Name of created variable: The name of the variables in which required JSON values will be stored. These are called “JSON Extractor variables”. Use a semi-colon to separate the names of the variables that will contain the results of JSON-PATH expressions. The number must match the number of JSON-PATH expressions.
  5. JSON Path Expressions: The JSON path expression needs to write here. You can write more than 1 JSON path expression which a semi-colon would separate. The JSON variables and JSON path expressions are synced in such a way that the first value is always allocated to the first variable. 
  6. Match No. (0 for random): If more than 1 string is matched in a response data and you need to use the JSON variable value which comes at a particular place (say 2nd place) then you have to give 2 as an input and JMeter will recognize all the matched values on the page but store only 2nd value in the variable. It is as same as the ordinal in the LoadRunner. ‘-1’ is used to capture all the values while ‘0’ is used to pick a random value from the list of match dynamic values. When all the values are extracted using -1 then the required value can be used as <variable name>_1, <variable name>_2 etc. 
  7. Compute concatenation var (suffix_ALL): This option is marked when many results are found. Then JMeter concatenates all the values using the ‘,’ separator and stores that value in a variable named <variable name>_ALL
  8. Default Value: If the JSON Path Expressions do not match, then the default value (e.g. JSON_value_Not_Found) can be set. It is very useful for debugging the script.

Learn with an Example:

Let’s consider, the JSON response contains the following values and I want to capture all the ‘size’ and ‘price’ values then I will write the $..size;$..prize in the JSON Path Expression field and the value will be captured in varSize and varPrize variables respectively.

{
    "shop": {
        "cloth": [
            {
                "category": "male",
                "type": "shirt",
                "size": 40,
                "price": 800
            },
            {
                "category": "male",
                "type": "jeans",
                "size": 32,
                "price": 1699
            },
			{
                "category": "female",
                "type": "top",
                "size": 32,
                "price": 699
            },
            {
                "category": "female",
                "type": "jeans",
                "size": 28,
                "price": 2999
            }
        ],
        "shoe": {
            "color": "black",
			"size": 7,
            "price": 2599
        }
    },
    "tax": 99
}
JMeter - JSON Extractor

You may be interested:


Leave a Comment