TypeScript to JSON Schema Generator

Convert TypeScript interfaces and types to JSON Schema (Draft 2020-12) for runtime validation

JSON Schema will appear here...

What is TypeScript to JSON Schema Generator?

TypeScript provides compile-time type safety, but when data crosses boundaries — API requests, configuration files, form submissions — you need runtime validation too. JSON Schema is the standard vocabulary for validating JSON documents, and it is supported by validators in every language. This tool bridges the gap by converting your TypeScript interface and type definitions into JSON Schema (Draft 2020-12). It handles optional properties, union types, string literal unions, nested objects, arrays, type references, and utility types like Partial, Required, Pick, Omit, and Record. The generated schema can be used with AJV, jsonschema, or any JSON Schema validator to verify that runtime data matches your TypeScript types.

How to Use

  1. Paste your TypeScript interface or type definitions into the left panel.
  2. Click "Generate JSON Schema" to parse the TypeScript and produce a Draft 2020-12 compliant schema.
  3. Review the output — referenced types appear under $defs and the main schema uses $ref to point to the first defined type.
  4. Copy the schema and use it with any JSON Schema validator like AJV, jsonschema, or Zod.

Why Use This Tool?

Bridge the gap between compile-time TypeScript types and runtime JSON validation
Supports interface and type alias definitions with optional properties, unions, and literals
Parses string literal unions as JSON Schema enums for cleaner, more specific validation
Handles nested objects, arrays, type references, and utility types (Partial, Required, Pick, Omit, Record)
Generates JSON Schema Draft 2020-12 compliant output that works with all standard validators

Tips & Best Practices

  • Use string literal unions for enum-like types: type Status = "active" | "inactive" — these become JSON Schema enum arrays.
  • Optional properties (?) are excluded from the "required" array, so they can be omitted in validated data.
  • Date types are converted to { type: "string", format: "date-time" } which most validators recognize.
  • Use $defs and $ref for reusable type definitions — the generator automatically extracts referenced types.
  • Validate with AJV for the best performance and Draft 2020-12 support: const ajv = new Ajv(); const validate = ajv.compile(schema);

Frequently Asked Questions

How are TypeScript types mapped to JSON Schema?

string becomes { type: "string" }, number becomes { type: "number" }, boolean becomes { type: "boolean" }, null becomes { type: "null" }, arrays (Type[]) become { type: "array", items: {...} }, and objects become { type: "object", properties: {...}, required: [...] }. Union types use anyOf, and string literal unions use enum arrays.

When should I NOT use this converter?

Avoid this tool when your TypeScript types use advanced features not supported by JSON Schema — such as conditional types, mapped types beyond the supported utility types, template literal types, or typeof references. Also, generic types with type parameters are not supported since JSON Schema has no concept of generics.

How are union types handled?

Union types are converted to JSON Schema's anyOf. String literal unions (like "admin" | "user") are converted to enum arrays for cleaner output and more specific validation. Mixed unions (e.g., string | number) use anyOf with the appropriate type schemas for each variant.

Why use $defs and $ref?

JSON Schema Draft 2020-12 uses $defs to define reusable schemas and $ref to reference them. This mirrors TypeScript's type reference semantics — when you define an Address interface and reference it in a User interface, the JSON Schema places Address under $defs and uses $ref: "#/$defs/Address" in the User schema.

How do I validate data with the generated schema?

Use any JSON Schema validator. With AJV: import Ajv from "ajv"; const ajv = new Ajv(); const validate = ajv.compile(schema); const isValid = validate(data). If isValid is true, data conforms to your TypeScript types. Other options include jsonschema (Python), json-schema-validator (Java), and online validators.

Is my TypeScript code sent to any server?

No. All parsing and schema generation runs entirely in your browser. Your TypeScript definitions and the generated JSON Schema never leave your device. No data is collected, stored, or transmitted to any server.

Real-world Examples

User API Response Schema

Convert a TypeScript User interface into JSON Schema for API response validation.

Input
interface User {
  id: number;
  name: string;
  email: string;
  role: "admin" | "editor" | "viewer";
  isActive?: boolean;
}
Output
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$ref": "#/$defs/User",
  "$defs": {
    "User": {
      "type": "object",
      "properties": {
        "id": { "type": "number" },
        "name": { "type": "string" },
        "email": { "type": "string" },
        "role": { "enum": ["admin", "editor", "viewer"] },
        "isActive": { "type": "boolean" }
      },
      "required": ["id", "name", "email", "role"],
      "additionalProperties": false
    }
  }
}

Form Validation Schema with Nested Types

Convert a TypeScript form interface with nested address type into JSON Schema for form validation.

Input
interface Address {
  street: string;
  city: string;
  zipCode: string;
}

interface OrderForm {
  customerName: string;
  address: Address;
  items: string[];
  notes?: string;
}
Output
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$ref": "#/$defs/OrderForm",
  "$defs": {
    "Address": {
      "type": "object",
      "properties": {
        "street": { "type": "string" },
        "city": { "type": "string" },
        "zipCode": { "type": "string" }
      },
      "required": ["street", "city", "zipCode"],
      "additionalProperties": false
    },
    "OrderForm": {
      "type": "object",
      "properties": {
        "customerName": { "type": "string" },
        "address": { "$ref": "#/$defs/Address" },
        "items": { "type": "array", "items": { "type": "string" } },
        "notes": { "type": "string" }
      },
      "required": ["customerName", "address", "items"],
      "additionalProperties": false
    }
  }
}

Related Tools