22

Is anyway to verify JSON Response got from RestFul API in JMeter?

I am getting below response:

{"workingfrom":[{"id":1234,"type":"office","name":"N1"},{"id":5678,"type":"home","name":"N2"}]}
Anand Somani
  • 801
  • 1
  • 6
  • 15

6 Answers6

14

JSON Assertion is another way (in JMeter 4). It can easily assert whether or not a node was present. It can even assert on the node value (regex or otherwise). enter image description here

Yarix
  • 1,261
  • 13
  • 22
singh2005
  • 1,251
  • 12
  • 19
5

Just put groovy-all-2.3.2.jar in the /lib directory of your JMeter installation and then you can use the Groovy JsonSlurper wrapper. After you start JMeter with that .jar in the path, then you can add a JSR223 Sampler that uses Groovy script.

Also, if you install the JMeter plugin packs, there is a JSON Path Extractor plugin, which has worked well for me.

These are 2 very nice alternatives to using regular expressions. If you are writing a lot of tests, either of these 2 methods will be superior.

djangofan
  • 28,471
  • 61
  • 196
  • 289
  • JSON Path Extractor looks really awesome. Thanks for the tip. I didn't even know there were plugins for JMeter. – rodrigo-silveira May 11 '16 at 12:17
  • A nice overview how to assert JSON responses in jMeter (incl. JSON Path Extractor) can be found here [http://eclipsesource.com/blogs/2014/06/12/parsing-json-responses-with-jmeter/](http://eclipsesource.com/blogs/2014/06/12/parsing-json-responses-with-jmeter/). – kabeleced May 12 '16 at 07:34
  • The latest JMeter 3.1 now includes Groovy .jar I think and so this is even easier now. – djangofan Jan 06 '17 at 17:16
3

You can use JMeter's JSON Plugins.

Install it first:

  1. Download the Plugins Manager JAR file and put it into JMeter's lib/ext directory. Then start JMeter and go to "Options" menu to access the Plugins Manager.
  2. On tab Available Plugins, select JSON Plugins, and click button Apply Changes and Restart JMeter.

Now add it to your test plan (or thread group, etc.): Dropdown menu Add -> Assertions -> jp@gc - JSON Path Assertion

Then configure it: Assuming that your data looks like {"version":"5.0.0","hitCount":23}

  1. JSON Path: $.hitCount
  2. Expected Value (regular expression): 23

Here is more information about JSON Path Assertion.

Yuci
  • 27,235
  • 10
  • 114
  • 113
  • FYI, [Deprecated in favor of core JMeter 4.0+] Allows extracting values from JSON responses using JSONPath syntax. Also ships JSONPath Assertion. – Jin Kwon Mar 09 '18 at 07:43
2

Add a Response Assertion and use a regex to validate the string. The regex you can use can be found elsewhere on Stackoverflow, here to be precise. Notice that this is a very costly operation and will probably eat your CPU if you do it with n concurrent threads.

Community
  • 1
  • 1
Sherlock
  • 7,525
  • 6
  • 38
  • 79
1

You can create variable and use it inside JSR223 Assertion if you want to compare values with response values

String code = vars.get("NumberOfUnit");
if(code >= "250" || code <="1000"){
    AssertionResult.setFailure(false);
}
else{
    AssertionResult.setFailure("Error");
    AssertionResult.setFailure(true);
}

Create a variable image - JMETER
Script - JMETER

0

I have similar json structure {"results": [{"types": [{"id": and I tried $.types without luck.

The correct JSON Path and value specification could be found in official documentation:

$.results[*].types

enter image description here

In your case, you could assert id value by:

$.workingfrom[*].id
Gryu
  • 2,102
  • 2
  • 16
  • 29