Skip to content

RestAssured – want to verify the body structure of JSON response in RestAssured

When i request for GET request, I’m getting the JSON response, but here my requirement is to validate the structure of response body.

For example:


The above response having structure, so i need to validate structure instead of one key value pair, how i can achieve this?



The best way is to verify json-schema matching.

Firstly, you need to add this dependency to your pom.xml

<!-- -->

Then you need to create a file json-schema-your-name.json with structure like that:

  "type": "object",
  "properties": {
    "data": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "flow_id": {
            "type": "string",
            "minLength": 36,
            "maxLength": 36
          "flow_org_id": {
            "type": "string"
        "required": [ "flow_id", "flow_org_id" ]

There are a bunch of services which generate schemas based on json – eg – this one

Once schema file is ready, you need to provide a path to your file in a String format – eg –

private static final String GET_SUBSCRIPTION_JSON_SCHEMA_PATH =

And invoke matchesJsonSchemaInClasspath("your/path/to/json-schema") method for assertion.


So the flow will basically be like:

  • you have a schema file somewhere in project dir (and know its path)
  • you hit the endpoint in some test method
  • you match the response you’ve received with the schema file

Practically, it will look following:

  public void someTestMethod() {
    Response responseToValidate = // here you should assign and store returned response
      .body("", equalTo("123"))

.body(matchesJsonSchemaInClasspath(“path/to/your/schema/in/string/format”)); }

8 People found this is helpful