← Back to Blog

JSON Schema Validation: A Practical Guide for API Developers

March 15, 2026 3 min read By CodeTidy Team

The Hidden Dangers of Invalid JSON Data

We've all been there - your API is humming along, processing requests and sending responses, when suddenly, a mysterious error crops up. After hours of debugging, you finally track down the culprit: a malformed JSON payload. JSON validation is a crucial step in ensuring the integrity of your API, but it's often overlooked until it's too late. In this article, we'll explore the world of JSON Schema and how to use it to validate your API data.

Table of Contents

  • What is JSON Schema?
  • Defining a JSON Schema
  • Validating Required Fields
  • Working with Nested Objects
  • Using $ref for Modularity
  • Putting it all Together with AJV

What is JSON Schema?

JSON Schema is a vocabulary that allows you to annotate and validate JSON documents. It's a powerful tool for ensuring that your API data conforms to a specific structure and format. By defining a schema, you can catch errors early on and prevent downstream problems.

Defining a JSON Schema

A JSON Schema is defined using a JSON object that describes the structure of your data. Here's an example of a simple schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "User",
  "type": "object",
  "properties": {
    "name": {"type": "string"},
    "email": {"type": "string", "format": "email"}
  },
  "required": ["name", "email"]
}

In this example, we define a schema for a User object that has two properties: name and email. We specify that both properties are required and that email must be a valid email address.

Validating Required Fields

One of the most common use cases for JSON Schema is validating required fields. Let's say we have an API endpoint that accepts a User object as input. We can use our schema to validate the input data and ensure that it contains all the required fields.

const Ajv = require('ajv');
const ajv = new Ajv();

const schema = {
  // our schema definition from above
};

const userInput = {
  name: 'John Doe',
  email: 'johndoe@example.com'
};

const valid = ajv.validate(schema, userInput);
if (!valid) {
  console.log(ajv.errors);
}

In this example, we use the AJV library to validate our user input against our schema. If the input data is invalid, AJV will return an array of error messages.

Working with Nested Objects

JSON Schema also supports nested objects. Let's say we have a schema for an Address object that looks like this:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Address",
  "type": "object",
  "properties": {
    "street": {"type": "string"},
    "city": {"type": "string"},
    "state": {"type": "string"},
    "zip": {"type": "string"}
  },
  "required": ["street", "city", "state", "zip"]
}

We can then reference this schema in our User schema using the $ref keyword:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "User",
  "type": "object",
  "properties": {
    "name": {"type": "string"},
    "email": {"type": "string", "format": "email"},
    "address": {"$ref": "#/definitions/Address"}
  },
  "required": ["name", "email"],
  "definitions": {
    "Address": {
      // our address schema definition from above
    }
  }
}

In this example, we define a User schema that has an Address property that references our Address schema.

Using $ref for Modularity

The $ref keyword is a powerful tool for creating modular schemas. By referencing external schemas, you can break up large schemas into smaller, more manageable pieces. This makes it easier to maintain and update your schemas over time.

Putting it all Together with AJV

AJV is a popular JavaScript library for validating JSON data against a schema. We've already seen how to use AJV to validate our user input against our schema. Here's an example of how to use AJV to validate a more complex schema:

const Ajv = require('ajv');
const ajv = new Ajv();

const schema = {
  // our user schema definition from above
};

const userInput = {
  name: 'John Doe',
  email: 'johndoe@example.com',
  address: {
    street: '123 Main St',
    city: 'Anytown',
    state: 'CA',
    zip: '12345'
  }
};

const valid = ajv.validate(schema, userInput);
if (!valid) {
  console.log(ajv.errors);
}

In this example, we use AJV to validate our user input against our schema. If the input data is invalid, AJV will return an array of error messages.

Key Takeaways

  • Use JSON Schema to validate your API data and catch errors early on.
  • Define a schema for each endpoint to ensure consistency and accuracy.
  • Use the $ref keyword to create modular schemas and break up large schemas into smaller pieces.
  • Use AJV to validate your schema and catch errors in your API.

FAQ

Q: What is the difference between JSON Schema and JSON validation?

A: JSON Schema is a vocabulary for annotating and validating JSON documents, while JSON validation is the process of checking that a JSON document conforms to a schema.

Q: Can I use JSON Schema with other programming languages?

A: Yes, JSON Schema is language-agnostic and can be used with any programming language that supports JSON.

Q: How do I handle errors when validating JSON data?

A: When validating JSON data, it's a good idea to handle errors in a way that provides useful feedback to the user. You can use AJV's error messages to provide detailed information about what went wrong.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp