JSON Schema Validation with Rest Assured (Updated 2020)

This entry is part 9 of 14 in the series Rest Assured Tutorial

When we are working with REST or HTTP based service API, we have the responsibility or we can say that it could come as a requirement to validate the behavior of the API. This we perform by validating the schema. For those who are not clear or familiar with what schema validation is? Schema validation ensures that the response coming back from the endpoint matches with the predefined set of rules. Note, that here we are not concerned about the data(values). Rest Assured provides the capability to test this feature using ‘Rest Assured Schema Validator’ and makes it very easy to test. In this example, we will learn “how to perform JSON schema validation with Rest-Assured?”.


Simple representation of validating JSON Schema with RESTAssured...!!! Click To Tweet

Steps to follow:

Step 1. Adding ‘Rest Assured Schema Validator‘ to the project

In order to perform this feature testing, first we need to setup the project environment by adding the required dependency to it. So, let’s add below ‘Rest Assured Schema Validator’ maven dependency in your project POM.

Note: Considering that you have already setup the maven project. But, if you are just using plain java project then download the required dependency jar file from this link and add it to the project classpath. See below screenshot:


Step 2. Generate a sample schema to test

Now, in order to test schema, we need to have one. right? Well, it’s pretty simple, just copy the JSON returned by any of your existing API and generate the schema using any Online schema generator tool like this.
Well, for testing purposes, we have below JSON file and using this above-mentioned tool, we will generate the schema.

Note: Schema is generally provided by developers to test.


JSON Schema:

Now, save this schema in a JSON format file. let’s say “JsonSchemaFile.JSON and add it into the classpath of your project.


Step 3. Test Code to perform the JSON schema validation with Rest Assured

Finally, let’s jump on the code:

As per the above code, the “matchesJsonSchemaInClasspath” method available in the newly added ‘Rest Assured Schema Validator’ API will help to test the structure of the JSON file passed as an argument here (in our case JsonSchemaFile.json).


That’s it, it’s that easy to perform JSON schema validation with REST Assured API 🙂

Simple representation of validating JSON Schema with RESTAssured...!!! Click To Tweet


If you like this post, please check out my other useful blog posts on Rest Assured:

Other Useful References:


Related Posts:

[wcp-carousel id=”5342″]
Series Navigation<< Serialization and De-Serialization using Rest AssuredHow to send POST XML Request using Rest Assured? >>

12 thoughts on “JSON Schema Validation with Rest Assured (Updated 2020)

  1. I tried your code but getting Exception in thread “main” java.lang.IllegalArgumentException: Schema to use cannot be null error. please help

    1. Hello Sonia,

      This error suggests that you must be missing the .json schema file meaning code couldn’t find your file at the location you’ve specified it your code.
      Please make your json file is present in the classpath.

      If it’s still happening, Kindly share your project folder structure and code so that I could help you better.


        1. It’s quite simple Apurva.

          Just add the .json file in the ‘resources’ folder of your project. That’s it.

          Let’s say. your project name is ‘JavaProject’, Ideally developers and testers tend to create at least these 2 folders in their build path (one is (src/main/java and another one is src/main/resources) or (src/test/java and another one is src/test/resources) respectively.

          Now, let’s say you are a tester, then your package will go like below:
          >src/test/java – {your packages}
          >src/test/resources – {your files}

          So, now if you understand, the resources folder is the one that holds any required files. So, in your case, just copy the JsonSchemaFile.json in this folder.

          That’s it.

          Kindly let me know if you still struggle with this.


  2. Hi Deepak Verma,
    Is there any option available in REST ASSURED to extract the generated JSON schema. I want use a different validator instead of the REST ASSURED validator. Please confirm.


    1. Hi Sathish,

      First of all, sorry for the late response.
      Well, Both the questions that you’ve mentioned are different.

      If you want to extract JSON schema or generate one, there is no option straight option within Rest Assured to do that. But, there are plenty of options available just like the one I mentioned above.

      And, In response to your validator question, Yes, you can use any validator, you don’t need to depend on Rest Assured validator.

      I hope my answer would be satisfactory to you. If not, Kindly let me know I’ll try to help you further.


  3. I tried the above code using the Jar file you mentioned. I am getting error for the method – matchesJsonSchemaInClasspath. The error is that the method matchesJsonSchemaInClasspath is undefined. My aim is to validate a json response as per the schema. I have 2 files. One is json response and other is schema.json. Kindly suggest how to do it.

    1. Hi Onkar,

      As per your requirement, this script will do the job for you to validate your response against your predefined schema structure.

      Well, It seems working fine to me. Could you please make sure that the .json is present at the classpath.

      And, please share your complete console log in the comment if it’s still showing the same error message. I would be able to better suggest you after checking the log.


Leave a Reply

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

Facebook Twitter Instagram YouTube