πŸ“JSON Schema & Validation
Estimated reading: 3 minutes 44 views

πŸ—οΈ JSON Schema Structure and Rules – Syntax Breakdown & Validation Guide (2025)


🧲 Introduction – How JSON Schema is Structured

JSON Schema is itself written in JSON format, and it provides a blueprint that describes the expected structure, data types, and validation rules for a JSON document. It’s like a contract that a JSON file must fulfill.

To use JSON Schema effectively, you must understand its key components and how to use them to define:

  • Data types (string, number, boolean, etc.)
  • Field requirements
  • Value constraints
  • Nested object or array validation

🎯 In this guide, you’ll learn:

  • The core building blocks of a JSON Schema
  • Rules for defining data types, structure, and restrictions
  • How to nest schemas and validate complex data
  • Syntax examples for each rule

βœ… Basic Structure of a JSON Schema

πŸ§ͺ Example – Skeleton JSON Schema

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "fieldName": {
      "type": "string"
    }
  },
  "required": ["fieldName"]
}

πŸ” Explanation:

  • $schema: Specifies the version of JSON Schema standard
  • type: Declares the type of the root object
  • properties: Defines the structure of child fields
  • required: Lists the fields that must exist

πŸ“‹ Common JSON Schema Keywords & Rules

1. type

Defines the data type of a value.

{ "type": "string" }

Valid types:

  • string
  • number
  • integer
  • boolean
  • object
  • array
  • null

2. properties

Used when the type is object. Defines each field and its schema.

{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer" }
  }
}

3. required

Lists keys that must be present in the JSON object.

"required": ["name", "age"]

4. items

Used when the type is array. Defines the schema for each array element.

{
  "type": "array",
  "items": {
    "type": "string"
  }
}

5. enum

Restricts a value to a specific set of allowed values.

{
  "type": "string",
  "enum": ["Pending", "Approved", "Rejected"]
}

6. format

Adds semantic validation for known data types like email, uri, or date-time.

{
  "type": "string",
  "format": "email"
}

7. minLength / maxLength (for strings)

{
  "type": "string",
  "minLength": 3,
  "maxLength": 50
}

8. minimum / maximum (for numbers)

{
  "type": "number",
  "minimum": 1,
  "maximum": 100
}

9. pattern (RegEx for strings)

{
  "type": "string",
  "pattern": "^[A-Z][a-z]+$"
}

Ensures the string starts with an uppercase letter and is followed by lowercase letters.


10. additionalProperties

Controls whether unknown fields are allowed.

"additionalProperties": false

Disallows any properties not listed under properties.


🧱 Full Example – JSON Schema with Rules

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "username": {
      "type": "string",
      "minLength": 5,
      "maxLength": 15
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "age": {
      "type": "integer",
      "minimum": 18
    },
    "roles": {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
  },
  "required": ["username", "email"],
  "additionalProperties": false
}

🧠 Best Practices for Using JSON Schema

PracticeBenefit
Use required to enforce key fieldsPrevents missing critical data
Use enum for controlled vocabulariesEnsures consistency in accepted values
Set additionalProperties: falsePrevents unapproved fields from being added
Use comments outside schema (in code)JSON doesn’t support native comments, so document externally

πŸ“Œ Summary – Recap & Next Steps

JSON Schema enables precise control over the structure and content of your JSON data. By learning its syntax and rules, you can prevent data quality issues and make your APIs more reliable.

πŸ” Key Takeaways:

  • JSON Schema defines types, structure, and validation rules for JSON data
  • Use keywords like type, properties, required, items, enum, etc.
  • Supports deep nesting, pattern matching, and value constraints
  • Ensures consistency and safety when handling JSON input/output

βš™οΈ Real-world use:

Used in API validation, frontend forms, configuration validation, and automated testing frameworks.


❓ FAQ – JSON Schema Rules


❓ Can I define optional fields in JSON Schema?
βœ… Yes. Any field not listed in required is considered optional.


❓ What does additionalProperties: false mean?
βœ… It disallows any extra fields not defined in the properties section.


❓ Can I validate arrays in JSON Schema?
βœ… Yes. Use type: "array" and define the item schema using items.


❓ Is it possible to reuse schemas across files?
βœ… Yes. JSON Schema supports $ref for referencing shared definitions.


Share Now :

Leave a Reply

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

Share

Json Schema structure and rules

Or Copy Link

CONTENTS
Scroll to Top