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:-
{ "$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": [ { "token": "abc123" } ], "required": [ "token" ], "properties": { "token": { "$id": "#/properties/token", "type": "string", "title": "The token schema", "description": "An explanation about the purpose of this instance.", "default": "", "examples": [ "abc123" ] } }, "additionalProperties": true }
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.