0
0
Postmantesting~15 mins

JSON Schema validation in Postman - Build an Automation Script

Choose your learning style9 modes available
Validate API response against JSON Schema
Preconditions (2)
Step 1: Send a GET request to the API endpoint https://api.example.com/users/1
Step 2: Wait for the response
Step 3: Validate the response body against the provided JSON Schema
✅ Expected Result: The response body matches the JSON Schema and validation passes
Automation Requirements - Postman test scripts
Assertions Needed:
Response status code is 200
Response body matches the JSON Schema
Best Practices:
Use pm.response.to.have.status for status code assertion
Use pm.response.to.have.jsonSchema for schema validation
Keep JSON Schema clear and minimal
Write clear and descriptive test names
Automated Solution
Postman
pm.test('Status code is 200', () => {
    pm.response.to.have.status(200);
});

const userSchema = {
    type: 'object',
    required: ['id', 'name', 'email'],
    properties: {
        id: { type: 'integer' },
        name: { type: 'string' },
        email: { type: 'string', format: 'email' }
    }
};

pm.test('Response matches JSON Schema', () => {
    pm.response.to.have.jsonSchema(userSchema);
});

The first test checks that the response status code is 200, which means the request was successful.

The userSchema defines the expected structure of the JSON response: an object with id as an integer, name as a string, and email as a string in email format.

The second test uses Postman's built-in jsonSchema assertion to verify the response body matches this schema exactly.

This approach ensures the API returns data in the correct format and helps catch errors early.

Common Mistakes - 3 Pitfalls
Not checking the response status code before schema validation
Using an incomplete or incorrect JSON Schema
Hardcoding values in the schema that change frequently
Bonus Challenge

Now add data-driven testing with 3 different user IDs (1, 2, 3) to validate their responses against the JSON Schema

Show Hint