Jmeter-Extract data using Postprocessor Part-I

Reading Time: 3 minutes

Jmeter is used to perform load testing. I have already discussed how it is easier and how can we use Jmeter in my earlier blogs. So let’s move some common problems of JMeter.Actually not a problem, but for many us it is difficult to extract data in Jmeter. Sometimes in Jmeter, we need to pass the data in the request externally. There are many options in Jmeter to pass the data externally in the request. External data might be passed through the database, any CSV file, Preprocessor, and Postprocessor. So there are two popular postprocessors in Jmeter. Using these postprocessors we can easily extract the data from the response. Also, we can pass that extracted data into the next request in Jmeter. In Jmeter-Extract Data from JSON using JSON Extractor and Regular Expression.

  1. JSON Extractor 
  2. Regular Expression.

There are some more postprocessors in JMeter but these are very easy and also work for a noncoding person. From these two postprocessors, we can extract any type of response. For example- JSON Data ,Html and text response,

So let’s move to first PostProcessor-

1. JSON Extractor- 

It is a handy tool to execute JSON Path expressions against JSON responses and storing the result into a JMeter Variable. If the response of any request is in JSON then we can use this extractor. Let’s see how can we use it in our script to extract the values from JSON.

  • Add Postprocessor-Json Extractor should be placed under the Http sampler which will run  before Json extractor.
  • Now JSON  Extractor contains these settings given below.

Name:- First, you can give a name to this post-processor.

Name of created variables:- Now set the variable name of the value which you need to extract so that can pass the variable in other requests.

JSON Path expression:– Add the JSON Syntax to extract content from the JSON response.

Match Number:– -1 for all, 0 for a random one, n for the nth on.

Default value:- This is the default value when extraction does not apply to the JSON document then this value will display there.

  • Check the JSON response in the view results tree listener by running the request.
  • Validate the JSON response using the JSON validator.
  • Copy that lint JSON response in the online tool –  jsonpath online evaluator 
  • Add the JSON path syntax to extract that particular value.

For example-

Let’s we have JSON-

{
	"name": "Joe",
	"age": 25,
	"phone": ["555-666-7777", "444-789-1234"],
	"homeAddress": [{
			"street": "Sycamore Avenue",
			"city": "Gilroy",
			"zipcode": "95046"
		},
		{
			"street": "Bailey Avenue",
			"City": "San Jose",
			"zipcode": "95141"
		}
	]
}

Now we have to extract the value -zip code of each city then we will use JSONPath syntax given below and check this in the JSON path extractor tool.

$.homeAddress[*].zipcode

Next, you can copy this JSON syntax into the Jmeter JSON extractor –>JsonPath Expression.Then use it in the requests with the help of variable name  ${Zip_code} .

Cheatsheet for JSON Syntax-

You can learn JSON syntax using this cheat sheet.

JSONPathDescription
$the root object/element
@the current object/element
. or []child operator
..recursive descent. JSONPath borrows this syntax from E4X.
*wildcard. All objects/elements regardless of their names.
[]subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON it is the native array operator.
[,]Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set.
[start:end:step]array slice operator borrowed from ES4.
?()applies a filter (script) expression.
()script expression, using the underlying script engine.

Second is Regular Expression. I will cover it in the next blog. Please follow my next blog, so that you will be able to know the use of the regular expression.

References :-

https://jmeter.apache.org/usermanual/index.html

Knoldus-blog-footer-image

Written by 

Aditi is Sr. Software Consultant -QA having experience of 3 years in Software Testing. She is familiar with API Testing & Performance Testing Using the tools like Jmeter,Postman, ReadyAPI(SoapUI),LoadUI are her expertise .Also having Knowledge of core concepts of manual and automation both as basic of Selenium Webdriver.She is always ready to adopt any environment and zeal to learn the new tools & technologies