Generate And Validate JSON Schema With JSON Response Using Postman

Introduction

JSON Schema is a contract for the JSON response that defines the expected data types and format of each field in the JSON response. In real-time scenario, JSON schema validation plays an important role to validate the JSON response with the expected data types and format. 

Description

In this article, I will show you the steps to generate JSON schema from JSON response and steps to validate JSON schema using Postman tool.

  1. What is JSON
  2. What is JSON Schema
  3. Why JSON Schema Validation required
  4. Generate Basic JSON Schema
  5. Steps for JSON Schema Validation in Postman

Before going through this article, visit my previous articles as mentioned below.

What is JSON

  • JSON stands for JavaScript Object Notation. It is a lightweight format for storing and transporting data. It is often used when data is sent from a server to a web page.

What JSON Schema

  • It is a specification for JSON based format for defining the structure of JSON data. JSON schema defines the expected data types and format of each field in the JSON response.

Why JSON Schema Validation required

  • It enables to validate your JSON structure and make sure it meets the required API.
  • Monitor your API responses, ensuring they adhere to a specified format.
  • Get alerted when breaking changes occur.
  • Construct a model of your API response Using JSON schema makes it easier to validate your API.

Generate Basic JSON Schema

Steps to be followed,

Step 1

The below Web API project is used for implementing GET request.

Generate And Validate JSON Schema With JSON Response Using Postman

The below localhost URL for GET request in Web API.

http://localhost:51259/api/employee

Send get request and check the response. The API response (JSON format) for GET request using Postman is shown below.

Generate And Validate JSON Schema With JSON Response Using Postman

Step 2

Then generate JSON schema from JSON response using the below link. Here I will copy the JSON response from Postman as shown below.

Generate And Validate JSON Schema With JSON Response Using Postman

Then using the below URL, We paste this response in the JSON scetion and click Generate schema from JSON button as shown below.

Generate And Validate JSON Schema With JSON Response Using Postman

JSON Response

[
    {
        "ID": 1,
        "FirstName": "Ram1",
        "LastName": "Pradhan1",
        "Gender": "Male",
        "Salary": 34000
    },
    {
        "ID": 2,
        "FirstName": "Satya1",
        "LastName": "Prakash",
        "Gender": "Male",
        "Salary": 13000
    },
    {
        "ID": 4,
        "FirstName": "Philip",
        "LastName": "Hastings",
        "Gender": "Male",
        "Salary": 45000
    },
    {
        "ID": 5,
        "FirstName": "Mary",
        "LastName": "Lambeth",
        "Gender": "Female",
        "Salary": 30000
    },
    {
        "ID": 8,
        "FirstName": "Satyaprakash",
        "LastName": "Samantaray",
        "Gender": "Male",
        "Salary": 10000
    },
    {
        "ID": 9,
        "FirstName": "Sourav",
        "LastName": "Ganguly",
        "Gender": "Female",
        "Salary": 20000
    },
    {
        "ID": 10,
        "FirstName": "Milan",
        "LastName": "Sahu",
        "Gender": "Male",
        "Salary": 12300
    },
    {
        "ID": 11,
        "FirstName": "Poonam",
        "LastName": "Dhilan",
        "Gender": "Female",
        "Salary": 22400
    },
    {
        "ID": 12,
        "FirstName": "Ritika",
        "LastName": "Sahu",
        "Gender": "Female",
        "Salary": 14300
    },
    {
        "ID": 13,
        "FirstName": "Jivan",
        "LastName": "Mohan",
        "Gender": "Male",
        "Salary": 24300
    },
    {
        "ID": 14,
        "FirstName": "Mohit",
        "LastName": "Chabara",
        "Gender": "Male",
        "Salary": 54000
    },
    {
        "ID": 15,
        "FirstName": "Minakshi",
        "LastName": "Rao",
        "Gender": "Female",
        "Salary": 12000
    },
    {
        "ID": 16,
        "FirstName": "Bill",
        "LastName": "Strong",
        "Gender": "Male",
        "Salary": 23000
    },
    {
        "ID": 17,
        "FirstName": "Seventeen1",
        "LastName": "Seventeen2",
        "Gender": "Female",
        "Salary": 77000
    }
]

The structure of this JSON response has a few important things to point out.

  1. The API returns a top-level array
  2. Each array entry is an object (an Employee).
  3. Each Employee has an ID, FirstName, LastName, Gender, and Salary.
  4. The FirstName, LastName, and Gender are strings, while the ID and Salary are integers.

JSON Schema

{
	"definitions": {},
	"$schema": "http://json-schema.org/draft-07/schema#", 
	"$id": "https://example.com/object1674730512.json", 
	"title": "Root", 
	"type": "array",
	"default": [],
	"items":{
		"$id": "#root/items", 
		"title": "Items", 
		"type": "object",
		"required": [
			"ID",
			"FirstName",
			"LastName",
			"Gender",
			"Salary"
		],
		"properties": {
			"ID": {
				"$id": "#root/items/ID", 
				"title": "Id", 
				"type": "integer",
				"examples": [
					1
				],
				"default": 0
			},
			"FirstName": {
				"$id": "#root/items/FirstName", 
				"title": "Firstname", 
				"type": "string",
				"default": "",
				"examples": [
					"Ram1"
				],
				"pattern": "^.*$"
			},
			"LastName": {
				"$id": "#root/items/LastName", 
				"title": "Lastname", 
				"type": "string",
				"default": "",
				"examples": [
					"Pradhan1"
				],
				"pattern": "^.*$"
			},
			"Gender": {
				"$id": "#root/items/Gender", 
				"title": "Gender", 
				"type": "string",
				"default": "",
				"examples": [
					"Male"
				],
				"pattern": "^.*$"
			},
			"Salary": {
				"$id": "#root/items/Salary", 
				"title": "Salary", 
				"type": "integer",
				"examples": [
					34000
				],
				"default": 0
			}
		}
	}

}

JSON Schema line by line, you can clearly see how it represents the shape of the /Employee response.

  1. The top-level type key tells us the API returns an "array".
  2. The items describe the shape of each array entry (each Employee).
  3. The FirstName, LastName, and Gender are strings, while the ID and Salary are integers.

Steps for JSON Schema Validation in Postman

Step 3

Under Tests tab of Postman, put your generated JSON schema inside const schema= {} as shown below,

Generate And Validate JSON Schema With JSON Response Using Postman

Step 4

Then add the below code snippet under const schema= {} as shown below.

Generate And Validate JSON Schema With JSON Response Using Postman

const schema= {
	//Put your JSON schema
}

pm.test("Validating.schema",()=>{
pm.response.to.have.jsonSchema(schema);
});

Step 5

Check the test result by sending the GET request. Here we can see the JSON schema validation is passed under Test Results tab. That means JSON schema is validated successfully.

Generate And Validate JSON Schema With JSON Response Using Postman

The detail on pm.test() function is shown below.

Generate And Validate JSON Schema With JSON Response Using Postman

Step 6

Check one scenario where the JSON schema validation error. In Salary array, I have changed the type from integer to string.

Once the changes are done then send the get request.

Generate And Validate JSON Schema With JSON Response Using Postman

Now, We can see that JSON schema validation is failed due to invalid type of Salary array in JSON schema.

Step 7

Now, If we undo the changes (i.e. change the type of Salary from string to integer) then the JSON schema validation is passed in the Test Results.

Generate And Validate JSON Schema With JSON Response Using Postman

Summary

In this write-up, we have learned the below details,

  1. What is JSON
  2. What is JSON Schema
  3. Why JSON Schema Validation required
  4. Generate Basic JSON Schema
  5. Steps for JSON Schema Validation in Postman

Thank You & Stay Tuned For More


Similar Articles