JSON Schema: Validating the Structure of Your Data

JSON Schema lets you define and enforce the structure of your JSON data, catching missing fields, wrong types, and invalid values before they cause bugs. This post explains the core keywords, shows a practical Python validation example using the jsonschema library, and explains why JSON Schema matt

JSON Schema: Validating the Structure of Your Data
Quick answer

JSON Schema is a vocabulary, itself written in JSON, that describes what valid JSON data must look like: which keys are required, the type of each value and limits such as minimums or allowed values. You validate incoming data against the schema, for example with Python's jsonschema library, which reports the rule that failed and where in the data it failed.

When you start working with JSON in real applications, you quickly run into a common problem: how do you know that the JSON data you receive actually has the structure you expect? What if a required field is missing? What if someone sends a string where you expected a number? This is exactly the problem that JSON Schema solves.

What Is JSON Schema?

JSON Schema is a vocabulary that allows you to describe the structure, content, and format of a JSON document. Think of it as a blueprint or contract for your data. You write a schema that defines what valid data looks like, and then you can validate any JSON document against that schema to confirm it meets your expectations.

JSON Schema is itself written in JSON, which keeps everything consistent and readable. It is widely supported across many programming languages, making it a practical choice for projects of any size.

A Simple Example

Imagine you are building an API that accepts user profile data. You expect each user object to have a name (string), an age (integer), and an email address (string). Here is what a JSON Schema for that might look like:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer",
      "minimum": 0
    },
    "email": {
      "type": "string",
      "format": "email"
    }
  },
  "required": ["name", "age", "email"]
}

Now consider this incoming JSON payload:

{
  "name": "Alex",
  "age": "twenty-five",
  "email": "[email protected]"
}

Validation against that schema would immediately flag a problem: age is a string, but the schema requires an integer. Without JSON Schema, this kind of bug might silently cause errors deep inside your application logic, making it hard to track down.

Key Keywords to Know

JSON Schema uses specific keywords to define rules. Here are the most common ones you will encounter:

  • type: Specifies the data type, such as string, number, integer, boolean, array, object, or null.
  • properties: Defines the expected keys in an object and their individual schemas.
  • required: A list of keys that must be present in an object.
  • minimum / maximum: Sets numeric bounds for number or integer types.
  • minLength / maxLength: Controls the length of string values.
  • enum: Restricts a value to a specific set of allowed values.
  • format: Provides semantic validation hints, such as email, date, or uri.
  • items: Defines the schema that each element in an array must follow.

Validating in Python

You do not need a specialised tool to use JSON Schema. In Python, the jsonschema library makes validation straightforward. Install it with:

pip install jsonschema

Then use it in your code like this:

import jsonschema
import json

schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer", "minimum": 0},
        "email": {"type": "string"}
    },
    "required": ["name", "age", "email"]
}

data = {
    "name": "Alex",
    "age": "twenty-five",
    "email": "[email protected]"
}

try:
    jsonschema.validate(instance=data, schema=schema)
    print("Validation passed!")
except jsonschema.ValidationError as e:
    print(f"Validation failed: {e.message}")

Running this script will output an error message explaining what failed and why (print e.path as well to see which field). That immediate, specific feedback is one of the biggest benefits of using JSON Schema in your projects.

Why This Matters in Real Projects

JSON Schema is not just for catching typos. It serves as living documentation for your data contracts, helps your team agree on data formats upfront, and makes APIs far more robust. Many tools, including Postman, OpenAPI (which powers tools like Swagger), and various CI/CD pipelines, have built-in JSON Schema support. Learning it now puts you in a strong position as your projects grow in complexity.

Free Resources to Go Deeper

JSON Schema: common questions

What is JSON Schema used for?

Checking that JSON data has the structure you expect before your code uses it, and documenting the data contract between systems or teams.

How do I make a field mandatory in JSON Schema?

List its key in the object's required array, for example "required": ["name", "age"].

How do I validate JSON against a schema in Python?

Install the jsonschema library, then call jsonschema.validate(instance=data, schema=schema). If the data is invalid it raises a ValidationError describing a problem; to list every problem at once, use a validator's iter_errors method.

What is the difference between type integer and number?

integer accepts whole numbers only; number accepts any numeric value, including decimals.

How do I limit a value to a fixed set of options?

Use the enum keyword with a list of the allowed values.

What's Next

Now that you understand how to validate the structure of your JSON data, the next logical step is exploring how JSON is used in real API communication. In the next post, we will look at how to make HTTP requests in Python and work with JSON responses from live APIs, putting everything you have learned into a practical, real-world workflow.

๐Ÿ”ง
If you are building or testing APIs that consume JSON, tools like Postman or Bruno let you write and run schema validation checks directly against your API responses, saving you from writing standalone test scripts just to catch structural errors. Postman, Bruno and Insomnia.