Back to Blog

JSON Schema Generator Tutorial

Create Schemas from JSON - Complete Guide

JSON Schema is a powerful tool for validating and documenting JSON data structures. Whether you're building APIs, working with configuration files, or ensuring data consistency, JSON Schema helps you define the structure your JSON must follow.

In this comprehensive tutorial, we'll show you how to generate JSON Schema from sample JSON, validate JSON against schemas, and use schemas in your applications. Use our free JSON Schema Generator to create schemas instantly.

What is JSON Schema?

JSON Schema is a specification for describing the structure of JSON data. It allows you to:

  • Define required and optional fields
  • Specify data types (string, number, boolean, object, array)
  • Set validation rules (min/max values, patterns, formats)
  • Document your data structure
  • Validate JSON at runtime

Try These Sample Data Examples

Copy these examples and paste them into our JSON Schema Generator to see how schemas are created:

User Object

Sample JSON:

{
  "id": 1,
  "name": "John Doe",
  "email": "john@example.com",
  "age": 30,
  "active": true,
  "createdAt": "2024-01-15T10:30:00Z"
}

Generated Schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "id": {
      "type": "integer"
    },
    "name": {
      "type": "string"
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "age": {
      "type": "integer",
      "minimum": 0
    },
    "active": {
      "type": "boolean"
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    }
  },
  "required": [
    "id",
    "name",
    "email",
    "age",
    "active",
    "createdAt"
  ]
}

💡 Tip: Copy the JSON above and paste it into our JSON Schema Generator to see the schema generation in action!

Product Array

Sample JSON:

[
  {
    "id": 1,
    "name": "Laptop",
    "price": 999.99,
    "inStock": true
  },
  {
    "id": 2,
    "name": "Mouse",
    "price": 25.5,
    "inStock": false
  }
]

Generated Schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "id": {
        "type": "integer"
      },
      "name": {
        "type": "string"
      },
      "price": {
        "type": "number"
      },
      "inStock": {
        "type": "boolean"
      }
    },
    "required": [
      "id",
      "name",
      "price",
      "inStock"
    ]
  }
}

💡 Tip: Copy the JSON above and paste it into our JSON Schema Generator to see the schema generation in action!

Nested Object

Sample JSON:

{
  "user": {
    "profile": {
      "firstName": "John",
      "lastName": "Doe",
      "address": {
        "street": "123 Main St",
        "city": "New York",
        "zipCode": "10001"
      }
    }
  }
}

Generated Schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "user": {
      "type": "object",
      "properties": {
        "profile": {
          "type": "object",
          "properties": {
            "firstName": {
              "type": "string"
            },
            "lastName": {
              "type": "string"
            },
            "address": {
              "type": "object",
              "properties": {
                "street": {
                  "type": "string"
                },
                "city": {
                  "type": "string"
                },
                "zipCode": {
                  "type": "string"
                }
              },
              "required": [
                "street",
                "city",
                "zipCode"
              ]
            }
          },
          "required": [
            "firstName",
            "lastName",
            "address"
          ]
        }
      },
      "required": [
        "profile"
      ]
    }
  },
  "required": [
    "user"
  ]
}

💡 Tip: Copy the JSON above and paste it into our JSON Schema Generator to see the schema generation in action!

Step-by-Step: Generate Your First Schema

1

Prepare Your Sample JSON

Start with a representative JSON object or array. Include all fields you want in your schema, even if some are null or empty.

{
  "name": "John",
  "email": "john@example.com",
  "age": 30
}
2

Choose Schema Format

Select JSON Schema Draft 7 (for validation) or OpenAPI Schema (for API documentation).

Draft 7

Best for validation and general use

OpenAPI

Best for API documentation

3

Generate Schema

Click Generate and our tool automatically analyzes your JSON structure, detects types, formats, and required fields.

4

Validate & Use

Use the built-in validator to test JSON against your schema, then copy or download the schema for use in your application.

Validation Examples

Valid JSON

This JSON matches the schema requirements:

{
  "name": "John",
  "email": "john@example.com",
  "age": 30
}

Invalid JSON

This JSON fails validation (invalid email format, wrong age type):

{
  "name": "John",
  "email": "invalid-email",
  "age": "thirty"
}

Errors: email format invalid, age should be integer not string

Common Use Cases

API Documentation

Generate schemas for API request/response bodies. Use with OpenAPI/Swagger for complete API documentation.

Data Validation

Validate user input, configuration files, or API responses against schemas to ensure data consistency.

Type Generation

Use schemas to generate TypeScript types, Python dataclasses, or other language-specific types automatically.

Testing

Create test data that matches your schema or validate test responses against expected schemas.

Generate JSON Schema Instantly

Use our free JSON Schema Generator to create schemas from your JSON in seconds. Supports Draft 7 and OpenAPI formats with built-in validation.

Try JSON Schema Generator Now

Related Resources