Pages

Monday, 30 January 2017

JMeter - PostProcessor - "JSON Extractor"

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 the information between the client and the server. It is written with the JavaScript object. Due to increase, 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 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 perform 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" element?

You can follow the below steps:
1. Select "Sampler" node whose response contain JSON value and you want to capture
2. Right-click on the node
3. Mouse hovers "Add"
4. Mouse hovers "Post Processors"
5. Click "JSON Extractor"

How to remove "JSON Extractor" element?
You can follow the below steps:
1. Select "JSON Extractor" node
2. Right click on "JSON Extractor" node
3. Click "Remove" (Mouse) or "Delete" button from the keyboard
4. Click "Yes"    

What are the attributes of "JSON Extractor" element?

"JSON Extractor" has the following attributes:
  • Name: To provide the name of the post-processor
  • Comments: To provide arbitrary comments (if any)
  • Apply to: To define the search scope of dynamic value. 
  •  Main sample and sub-samples: In case, the request is redirected then use this search scope, so that required JSON value can be searched in the response of both main and re-directed request.
  • Main sample only: When request is not re-directed or JSON value is present only in the response of main request then use this search scope.
  • Sub-samples only: When request is re-directed and required JSON value is available in the response of re-directed request then you can use this search scope.
  • 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.
  • 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.
  • JSON Path Expressions: The JSON path expression needs to write here. You can write more than 1 JSON path expression which would be separated by a semi-colon. The JSON variables and JSON path expressions are synced in such a way that first value always allocated to the first variable. 
  • Match No. (0 for random): If more than 1 strings are 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. 
  • 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
  • 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 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
}



Next Topic:

Apache JMeter - "Assertions"

Previous Topic:
Apache JMeter - "Boundary Extractor" Element

Related Topics:



No comments:

Post a Comment