Sitemap

JSON Schema Validation via Postman

4 min readFeb 27, 2025

What is JSON Schema?

JSON Schema is a structured format for defining the expected structure of JSON data. It provides a clear, machine-readable contract for API responses, ensuring that the data adheres to predefined rules such as required fields, data types, and value constraints. JSON Schema helps developers validate and document JSON responses in a standardized way.

Example JSON Schema

Here is an example JSON Schema that defines the expected structure of a user object:

{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"email": { "type": ["string", "null"] },
"isActive": { "type": ["boolean", "null"] }
},
"required": ["id", "name", "email"]
}

This schema ensures that every user object has an id (integer), name (string), and email (which can be either a string or null). The isActive field is optional but can only be a boolean or null.

What is JSON Schema Validation?

JSON Schema validation is the process of verifying that a JSON response conforms to a given schema. This validation ensures consistency and correctness in API responses, preventing errors due to missing or incorrect data structures. Tools like Postman allow users to implement schema validation within their API testing workflow to automatically verify API responses against predefined JSON Schemas.

What is NOT JSON Schema Validation?

JSON Schema validation is not the same as contract testing. While schema validation ensures that API responses adhere to a predefined structure, contract testing verifies that an API behaves as expected across different environments and integrations. Some people confuse the two, but JSON Schema validation focuses solely on data format validation rather than full API behavior verification.

Benefits of JSON Schema Validation

  1. Ensures Data Integrity: By validating JSON responses against a schema, you can ensure that all required fields are present and have the correct data types.
  2. Improves API Reliability: Detects inconsistencies early in the development cycle, preventing issues before they reach production.
  3. Enhances Readability and Maintainability: Provides a clear contract for API responses, making it easier for teams to understand expected data structures.
  4. Automates API Testing: Can be integrated into automated testing workflows, reducing manual effort in verifying API responses.
  5. Prevents Breaking Changes: Helps maintain backward compatibility by ensuring that API responses adhere to a consistent structure over time.

Implementing JSON Schema Validation in Postman

Postman allows users to validate API responses against a predefined JSON Schema using JavaScript and the Ajv (Another JSON Schema Validator) library. Below is an example script that performs JSON Schema validation within a Postman test:

const Ajv = require('ajv'),
ajv = new Ajv({logger: console, allErrors: true}),
schemaStr = pm.environment.get("response_schema"),
RESPONSE_SCHEMA = JSON.parse(schemaStr),
jsonData = pm.response.json()

// Validate against the schema
pm.test('Schema is valid', function() {
pm.expect(ajv.validate(RESPONSE_SCHEMA, jsonData), JSON.stringify(ajv.errors)).to.be.true
});

Storing JSON Schemas in Postman Environment Variables

Keeping JSON Schemas in Postman environment variables improves script readability and maintainability, especially when dealing with long and complex schemas. Instead of embedding large schema definitions directly in test scripts, they can be stored in environment variables and referenced dynamically.

I generally use this website to generate schemas, but if there is a chance to obtain them from backend developers, it would be more accurate. Additionally, I typically use type checks in this format: ["string","null"], ["boolean","null"] if that field can potentially be null. I do this to prevent unnecessary failures because the validation would fail if the field is null instead of the defined type.

To convert a multiline JSON schema into a single-line format, you can use the following Python script:

import json


# Function to convert JSON to a single line
def json_to_single_line(json_data):
return json.dumps(json_data, separators=(',', ':'))


# Read multiline JSON data from console input
print("Enter your JSON data (end input with an empty line and 'end'):")

# Collect the entire multiline JSON input
json_input = ""
while True:
line = input()
if line.strip().lower() == 'end': # end input with the word 'end'
break
json_input += line + "\n" # append each line to the JSON string

try:
# Parse the JSON data from the collected multiline input string
parsed_json = json.loads(json_input)

# Convert to a single line
single_line_json = json_to_single_line(parsed_json)

print("Single line JSON:")
print(single_line_json)
except json.JSONDecodeError as e:
print(f"Invalid JSON input: {e}")

By converting JSON Schemas into a single-line format, you can store them in Postman environment variables more efficiently, reducing clutter in your test scripts.

Conclusion

JSON Schema validation in Postman is a powerful technique for ensuring API responses are structured correctly. By integrating schema validation into your testing workflow, you can improve API reliability, maintainability, and prevent breaking changes. Storing schemas as environment variables and using automation scripts further enhances the efficiency of this approach.

--

--

Serhat Ozdursun
Serhat Ozdursun

Written by Serhat Ozdursun

QA Automation Engineer at Index

No responses yet