JMeter JSON Extractor with Multiple Conditions in Expression: JMeter is used to create performance scripts to behave like real user. We need to send some data that should be captured in previous requests and need to send in further requests this process is called Correlation.

For doing correlation in JMeter we have multiple options like regular expression extractor, Xpath Extractor, Json Extractor, CSS Extractor.

We need to use the extractor based on the response type and the value what we are capturing

  • If your response is Html content type we can use CSS extractor or regular expression or Xpath extractor
  • If your response body is in XML content then we can use XPath extractor
  • If your data is in JSON format then we need to go with JSON Expression Extractor

JSON Expression Extractor is one of the best to capture the JSON content without out any huddle

If below is my response body then

{
   "students": [
     {
       "id": 5331,
       "firstName": "max",
       "lastName": "well"
     },
     {
       "id": 3333,
       "firstName": "max",
       "lastName": "watson"
     },
     {
       "id": 5321,
       "firstName": "john",
       "lastName": "smith"
     }
   ],
   "city": "Newyork"
 }

 

Now if you want to capture all values of Id Then below will be the Expression

$..students.[*].id or $..id

Output will be

Result[0]=5331
Result[1]=3333
Result[2]=5321

If you need specific index you can get using an index like below expression

 $..students.[1].id 

If you want the Id of the student using some condition like where the first name or last name we can use & amd | Operator in expressions

 $..students[?(@.firstName == "max")] 

In above expression we are fetching JSON objects when the first name equals to max output will be

 Result[0]={"firstName":"max","lastName":"well","id":5331}
Result[1]={"firstName":"max","lastName":"watson","id":3333} 

if you need to get JSON object with both conditions or with multiple conditions below will be the expression

$..students[?(@.firstName == "max" && @.lastName == "watson")]

Output is

Result[0]={"firstName":"max","lastName":"watson","id":3333}

if we need the id property from captured JSON object use below expression

$..students[?(@.firstName == "max" && @.lastName == "watson")].id

output will be

Result[0]=3333

we can create multiple && or || conditions on string,int,decimal data type with different operators like less than and greater than, less than equal to, greater than equal to operators

Thanks

Also Read: Decode JWT Token in Jmeter

JMeter JSON Extractor with Multiple Conditions in Expression

Leave a Reply

Your email address will not be published. Required fields are marked *