Make Selenium Easy

REST Assured Tutorial 54 – What is JSON Schema?

Table of Contents

Introduction

As a part of End to End REST Assured Tutorial, in this post, we will learn about JSON schema.

JSON Schema

Let’s look at two images of the car below:-

When we think of a car then we visualize a structure of it. For example:- A car has four wheels, a body, fuel tank, steering, seats, etc. These are a part of the basic structure of any car. If an object which has only 3 wheels but satisfies all remaining basic structures can not be called a car.

Not all fields are mandatory while creating an account on Gmail but if you don’t give a value for mandatory fields you get an error. Similarly, a JSON that stores data as a key-value pair may have a structure that defines its allowed format, fields, values, etc. That structure can be called a JSON Schema.

A JSON Schema is a JSON document that can validate a JSON object syntactically and semantically. Syntactical validation includes if a JSON object is a valid JSON object. Semantical validations include if a valid JSON object includes all mandatory fields, the format of value, allowed values, etc.

As per the official page of JSON Schema, JSON Schema is a vocabulary that allows you to annotate and validate JSON documents. This website contains in-depth knowledge of JSON Schema.

Example JSON Schema:-

Above is a very simple example of JSON Schema. Focus on keys “required” and “properties“. Any JSON object against this JSON schema must have a “token” property in it and the type of value of key “token” must be a string as defined in the “properties” section. In short, whatever keys are present under “required” array, a JSON object corresponding to it must have those keys. “properties” section defines metadata about keys.

additionalProperties” key is to represent if extra properties other than listed under “properties” section are allowed or not.

I think understanding JSON schema is quite easy. We can put validation for a property as well like min or max value or a regex.

You can use JSON Schema website to generate a JSON schema of a given JSON object and modify it as you wish.

As a tester, we must be aware of the basic concepts of JSON Schema creation. It is very useful during automation to assert if JSON request and response payload are expected or not. Not every time developer will provide you a JSON Schema. In that case, you must know how can you create JSON schema by yourselves and validate multiple things directly.

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

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

REST Assured Tutorial 54 – What is JSON Schema?

by Amod Mahajan time to read: 2 min
0
%d bloggers like this: