API Testing: JSON Schema validation in Postman

It is important to validate the JSON response schema while doing API testing.
There are many reasons for this:

  1. If any fields in json response changes it will detect the change
  2. If any data type of any field is changed, it will detect the change
  3. If any contract is changed it will detect the change

Hence it is always important to have this check in place.
We will take this example in the popular tool POSTMAN.

Most of us use POSTMAN while testing API and POSTMAN has the capability to run automated checks.

Here is how we can validate json schema:

Step1: Get the response json and convert it into schema using online json to schema converter tool
Lets take an example of below simple json response of Auth token.

{
    "access_token": "f093d759-8f15-4d62-afab-f9ccf71577db",
    "token_type": "bearer",
    "expires_in": 1799
}

Go to https://jsonschema.net/ and paste the json in the left text box.
Make sure to select setting and check the Numeric as JSON type.
It will convert numeric as number not as integer.

Then save and click on submit button.

Step2: Copy the generated Schema

Step3: Go to Postman and go to the request – Test tab.

var Ajv = require('ajv'),
    ajv = new Ajv({logger: console}),
    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": {},
    "additionalProperties": true,
    "required": [
        "access_token",
        "token_type",
        "expires_in"
    ],
    "properties": {
        "access_token": {
            "$id": "#/properties/access_token",
            "type": "string",
            "title": "The Access_token Schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": [
                "cd42f000-9905-4971-840b-a66fa9dac4a4"
            ]
        },
        "token_type": {
            "$id": "#/properties/token_type",
            "type": "string",
            "title": "The Token_type Schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": [
                "bearer"
            ]
        },
        "expires_in": {
            "$id": "#/properties/expires_in",
            "type": "number",
            "title": "The Expires_in Schema",
            "description": "An explanation about the purpose of this instance.",
            "default": 0,
            "examples": [
                1799.0
            ]
        }
    }
};
 
pm.test('Schema is valid', function() {
    var data = pm.response.json();
    pm.expect(ajv.validate(schema, data)).to.be.true;
});

Once this code snippet is pasted in postman, hit send request button.
it will validate and result in Pass in Test result tab of response.

Leave a Reply