JSON Schema

A JSON Schema is a special JSON document that describes the structure, format, and rules that a JSON data object must follow. In simple words, it is a blueprint or template that defines what valid JSON data should look like.

Think of JSON Schema like a job application form. The form defines which fields are required (like Name and Phone Number), what type of data goes in each field (Name must be text, Age must be a number), and what values are acceptable (Age must be between 18 and 65). If an applicant fills the form incorrectly, it gets rejected. JSON Schema works in exactly the same way for data.

Why is JSON Schema Needed?

Without validation, any data could be submitted or stored — a text where a number is expected, a missing required field, or a value outside the accepted range. This causes bugs, data corruption, and security issues. JSON Schema solves this by:

  • Ensuring data has the correct structure before it is processed
  • Providing clear documentation about what data is expected
  • Enabling automatic validation on both the client (browser) and server side
  • Making APIs more reliable and predictable

Structure of a JSON Schema

A JSON Schema is itself written in JSON. It uses special reserved keywords to describe what the data should look like. The most important keywords are:

KeywordPurpose
$schemaDeclares which version of JSON Schema is being used
titleA human-readable name for the schema
descriptionA brief explanation of what the schema represents
typeSpecifies the data type (string, number, boolean, object, array, null)
propertiesDefines the allowed keys and their rules inside an object
requiredLists which keys must always be present
minimum / maximumSets the minimum and maximum value for numbers
minLength / maxLengthSets the minimum and maximum length for strings
enumRestricts a value to a specific list of allowed values

A Simple JSON Schema Example

Below is a schema that defines what a valid student record should look like:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Student",
  "description": "Schema for a student record",
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "minLength": 2,
      "maxLength": 50
    },
    "age": {
      "type": "integer",
      "minimum": 5,
      "maximum": 30
    },
    "grade": {
      "type": "string",
      "enum": ["A", "B", "C", "D", "F"]
    },
    "isEnrolled": {
      "type": "boolean"
    }
  },
  "required": ["name", "age", "isEnrolled"]
}

Understanding the Schema Above

  • The data must be an object (type: object)
  • name must be a string between 2 and 50 characters
  • age must be an integer between 5 and 30
  • grade must be one of the values: A, B, C, D, or F
  • isEnrolled must be a boolean (true or false)
  • The fields name, age, and isEnrolled are required — they must always be present

Valid JSON Data for the Above Schema

{
  "name": "Ishaan Tiwari",
  "age": 18,
  "grade": "B",
  "isEnrolled": true
}

Invalid JSON Data Examples and Why They Fail

Missing required field:

{
  "name": "Ishaan Tiwari",
  "grade": "B"
}
// INVALID: "age" and "isEnrolled" are required but missing

Wrong data type:

{
  "name": "Ishaan Tiwari",
  "age": "eighteen",
  "isEnrolled": true
}
// INVALID: "age" must be a number, not a string

Value out of allowed range:

{
  "name": "Ishaan Tiwari",
  "age": 55,
  "isEnrolled": true
}
// INVALID: "age" exceeds the maximum allowed value of 30

Value not in enum list:

{
  "name": "Ishaan Tiwari",
  "age": 18,
  "grade": "E",
  "isEnrolled": true
}
// INVALID: "grade" value "E" is not in the allowed list [A, B, C, D, F]

JSON Schema for an Array

Schemas can also validate arrays and define what each item in the array should look like:

{
  "type": "array",
  "items": {
    "type": "string"
  },
  "minItems": 1,
  "maxItems": 5
}

This schema says the value must be an array of strings with at least 1 and at most 5 items.

Online Tools to Validate JSON Schema

There are free online tools where a JSON Schema and JSON data can be tested together to check if the data is valid:

  • jsonschemavalidator.net — paste the schema and data and click Validate
  • jsonlint.com — also helps check if JSON syntax itself is correct
  • JSON Schema Validator at json-schema.org — official resource with examples

Key Points to Remember

  • JSON Schema is a blueprint that describes the expected structure of JSON data
  • It is itself written in JSON format
  • Common keywords: type, properties, required, minimum, maximum, enum, minLength, maxLength
  • Schemas help catch errors early before bad data enters a system
  • Both APIs and databases use JSON Schema for validation

Summary

JSON Schema is a powerful tool that acts as a quality gate for data. By defining exactly what valid data looks like — required fields, correct types, allowed values, and length limits — JSON Schema ensures that applications receive clean, predictable, and reliable data. While beginners may not write schemas daily, understanding them is essential for building professional-grade APIs and data-driven applications.

Leave a Comment

Your email address will not be published. Required fields are marked *