Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 56 – JSON Schema Validation Without Rest Assured

Posted on 02/08/2025 By admin

As a part of End to End REST Assured Tutorial, in this post, we will learn about JSON Schema validation without Rest Assured.

We have already covered JSON Schema Validation In Rest Assured. You may be wondering why do we need to validate JSON schema without Rest Assured. We just not validate JSON response schema but we should validate JSON request payload as well just to ensure if the payload is correct. If we create it dynamically or get it through external sources we must have a schema check before we pass that payload to API.

We may need to verify schema of any JSON and it should not be restricted only through Rest Assured.

We can use json-schema-validator standalone as well. There is no need to use it with Rest Assured only. Something like TestNG can be integrated with Selenium or Rest Assured or any other libraries. We need to add Hamcrest (A Core API and libraries of hamcrest matcher framework) explicitly if want to use it without Rest Assured.

I am using the below version of json-schema-validator and hamcrest:-

  io.rest-assured json-schema-validator 4.3.1

  org.hamcrest hamcrest 2.1 test

Since schema validation methods of json-schema-validator library create a hamcrest matcher, we must need to add Hamcrest library in our project. Now we can validate schema of any JSON without Rest Assured.

I have stored the below JSON schema outside of resource folder.

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "object",
    "title": "The root schema",
    "description": "The root schema comprises the entire JSON document.",
    "default": {},
    "examples": [
        {
            "firstname": "Jim",
            "lastname": "Brown",
            "totalprice": 111,
            "depositpaid": true,
            "bookingdates": {
                "checkin": "2018-01-01",
                "checkout": "2019-01-01"
            },
            "additionalneeds": "Breakfast"
        }
    ],
    "required": [
        "firstname",
        "lastname",
        "totalprice",
        "depositpaid",
        "bookingdates",
        "additionalneeds"
    ],
    "properties": {
        "firstname": {
            "$id": "#/properties/firstname",
            "type": "string",
            "title": "The firstname schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": [
                "Jim"
            ]
        },
        "lastname": {
            "$id": "#/properties/lastname",
            "type": "string",
            "title": "The lastname schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": [
                "Brown"
            ]
        },
        "totalprice": {
            "$id": "#/properties/totalprice",
            "type": "integer",
            "title": "The totalprice schema",
            "description": "An explanation about the purpose of this instance.",
            "default": 0,
            "examples": [
                111
            ]
        },
        "depositpaid": {
            "$id": "#/properties/depositpaid",
            "type": "boolean",
            "title": "The depositpaid schema",
            "description": "An explanation about the purpose of this instance.",
            "default": false,
            "examples": [
                true
            ]
        },
        "bookingdates": {
            "$id": "#/properties/bookingdates",
            "type": "object",
            "title": "The bookingdates schema",
            "description": "An explanation about the purpose of this instance.",
            "default": {},
            "examples": [
                {
                    "checkin": "2018-01-01",
                    "checkout": "2019-01-01"
                }
            ],
            "required": [
                "checkin",
                "checkout"
            ],
            "properties": {
                "checkin": {
                    "$id": "#/properties/bookingdates/properties/checkin",
                    "type": "string",
                    "title": "The checkin schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": "",
                    "examples": [
                        "2018-01-01"
                    ]
                },
                "checkout": {
                    "$id": "#/properties/bookingdates/properties/checkout",
                    "type": "string",
                    "title": "The checkout schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": "",
                    "examples": [
                        "2019-01-01"
                    ]
                }
            },
            "additionalProperties": true
        },
        "additionalneeds": {
            "$id": "#/properties/additionalneeds",
            "type": "string",
            "title": "The additionalneeds schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": [
                "Breakfast"
            ]
        }
    },
    "additionalProperties": true
}
package JsonSchema;

import java.io.File;

import org.hamcrest.MatcherAssert;
import org.testng.annotations.Test;

import io.restassured.module.jsv.JsonSchemaValidator;


public class VerifyJsonSchemaWithoutRestAssured {
        
        @Test
        public void verifyJsonSchemaWithoutRestAssured()
        {
                String json = "{\r\n" + 
                                "    \"firstname\" : \"Jim\",\r\n" + 
                                "    \"lastname\" : \"Brown\",\r\n" + 
                                "    \"totalprice\" : 111,\r\n" + 
                                "    \"depositpaid\" : true,\r\n" + 
                                "    \"bookingdates\" : {\r\n" + 
                                "        \"checkin\" : \"2018-01-01\",\r\n" + 
                                "        \"checkout\" : \"2019-01-01\"\r\n" + 
                                "    },\r\n" + 
                                "    \"additionalneeds\" : \"Breakfast\"\r\n" + 
                                "}";
                
                MatcherAssert.assertThat(json, JsonSchemaValidator.matchesJsonSchema(new File("C:\\Users\\amomahaj\\git\\master\\src\\test\\java\\JsonSchema\\SampleJsonSchemaCreateBooking.json")));
        }

}

If you keep the expected JSON schema in resource folder then you can use matchesJsonSchemaInClasspath also.

You can download/clone the above sample project from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Compare JSONs
Next Post: TestNG Tutorials 46: Overriding Parameters in TestNG

Related Posts

image – Make Selenium Easy Uncategorized
Setup of selenium project and How to open a chrome browser – Make Selenium Easy Uncategorized
TestNG Tutorials 2: Installation Of TestNG In Eclipse ==> Download And Add To Build Path Way | Make Selenium Easy Uncategorized
REST Assured Tutorial 73 – How to ignore node/s for JSON comparison in JSONassert Uncategorized
TestNgInLeftSide – Make Selenium Easy Uncategorized
REST Assured Tutorial 34 – Serialization – Java Object To JSON Object Using Gson API Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark