SyntaxSnap

TypeScript → JSON Schema

Paste your TypeScript interfaces and instantly generate standards-compliant JSON Schema (Draft 7) definitions. Perfect for API contracts, form validation, and code generation.

TypeScript Input
0.7KB
JSON Schema Output
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "User",
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    },
    "email": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
    "age": {
      "type": "number"
    },
    "isActive": {
      "type": "boolean"
    },
    "role": {
      "type": "string",
      "enum": [
        "admin",
        "user",
        "guest"
      ]
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "address": {
      "type": "object",
      "properties": {
        "street": {
          "type": "string"
        },
        "city": {
          "type": "string"
        },
        "zip": {
          "type": "string"
        }
      },
      "required": [
        "street",
        "city"
      ]
    }
  },
  "required": [
    "id",
    "email",
    "isActive",
    "role",
    "tags",
    "address"
  ],
  "$defs": {
    "BlogPost": {
      "type": "object",
      "properties": {
        "id": {
          "type": "number"
        },
        "title": {
          "type": "string"
        },
        "content": {
          "type": "string"
        },
        "author": {
          "$ref": "#/$defs/User"
        },
        "tags": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "publishedAt": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ]
        },
        "metadata": {
          "type": "object",
          "additionalProperties": {}
        }
      },
      "required": [
        "id",
        "title",
        "content",
        "author",
        "tags",
        "publishedAt",
        "metadata"
      ]
    },
    "Status": {
      "type": "string",
      "enum": [
        "active",
        "inactive",
        "pending"
      ]
    },
    "Product": {
      "type": "object",
      "properties": {
        "productId": {
          "type": "string"
        },
        "price": {
          "type": "number"
        },
        "inStock": {
          "type": "boolean"
        },
        "description": {
          "type": "string"
        },
        "categories": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "dimensions": {
          "type": "object",
          "properties": {
            "width": {
              "type": "number"
            },
            "height": {
              "type": "number"
            },
            "depth": {
              "type": "number"
            }
          },
          "required": [
            "width",
            "height"
          ]
        }
      },
      "required": [
        "productId",
        "price",
        "inStock",
        "categories",
        "dimensions"
      ]
    }
  }
}

Why Convert TypeScript to JSON Schema?

Cross-Language Contracts

TypeScript types only exist at compile time and are exclusive to the JavaScript ecosystem. JSON Schema is a language-agnostic standard for describing data structures. By converting your interfaces to JSON Schema, you create contracts that can be consumed by Python, Go, Java, Rust, and any other language with a JSON Schema validator.

API Documentation & Validation

JSON Schema is the foundation of OpenAPI specifications. Convert your TypeScript request/response types to JSON Schema and drop them directly into your openapi.yaml components section. Validators like Ajv can then enforce these schemas at runtime on both client and server.

Common Use Cases

  • Generate OpenAPI component schemas from TypeScript interfaces
  • Validate API payloads at runtime with Ajv or similar validators
  • Create cross-language data contracts for microservices
  • Feed JSON Schema into code generators for Python, Go, or Rust
  • Define form validation rules from existing TypeScript models
  • Publish schema registries for event-driven architectures
  • Generate database migration schemas from application types
  • Build configuration file validators from TypeScript configs
  • Auto-generate documentation from type definitions

How It Works

This tool performs lightweight AST-style tokenization and parsing of your TypeScript source code entirely in the browser. It extracts interface and type alias declarations, then recursively maps each property type to its JSON Schema equivalent:

  • Primitives (string, number, boolean) → { "type": "string" }
  • Optional properties (name?) → omitted from required array
  • String literal unions ("a" | "b") → { "enum": ["a", "b"] }
  • Arrays (string[]) → { "type": "array", "items": ... }
  • Nested objects → Recursive { "type": "object", "properties": ... }
  • Multiple types → $defs + $ref cross-references

All processing happens locally in your browser — no code is uploaded to any server, ensuring your proprietary type definitions remain completely private.

Explore More Developer Tools

Boost your productivity with our other privacy-first utilities.

View all Developer tools →

Popular Developer Tools