DevTools Logo

JSON Schema to TypeScript

JSON Schema to TypeScript

Compile draft-07 JSON Schema into deterministic TypeScript interfaces

Schema input

About

Paste a JSON Schema draft-07 subset and generate readable TypeScript interfaces. Required and optional fields, enums, const values, unions, arrays, nested objects, descriptions, additionalProperties, and cyclic $ref definitions are handled locally with deterministic output.

    Examples

    Required vs optional with a union

    Input
    { "type": "object", "required": ["id"], "properties": { "id": { "type": "integer" }, "value": { "anyOf": [{ "type": "string" }, { "type": "number" }] } } }
    Output
    export interface Root {
      id: number;
      value?: string | number;
    }

    Listed required keys lose the ?, unlisted keys gain it, and anyOf branches join with |.

    Cyclic $ref plus a string map

    Input
    { "type": "object", "properties": { "node": { "$ref": "#/$defs/Node" } }, "additionalProperties": { "type": "string" }, "$defs": { "Node": { "type": "object", "required": ["next"], "properties": { "next": { "$ref": "#/$defs/Node" } } } } }
    Output
    export interface Node {
      next: Node;
    }
    
    [key: string]: string;

    Node emits exactly once with next: Node, and additionalProperties becomes an index signature.

    About this tool

    JSON Schema to TypeScript compiles a JSON Schema object into TypeScript interfaces with compileJsonSchemaToTypescript, defaulting the root to export interface Root. Object properties are emitted in sorted key order with required entries as name: T and everything else as name?: T, descriptions become /** */ doc comments, and integer maps to number. Unions come from anyOf/oneOf (joined with |), string|number style type arrays, enum string literals, and const values, while arrays render as T[] or Array<A | B> when the item type is itself a union.

    References resolve through $defs and definitions: $ref values shaped like #/$defs/Name or #/definitions/Name (with ~0/~1 unescaping) emit one interface per target name, and # points back at the root. Cyclic graphs terminate because emit tracks an active set — a self-referencing Node with required next: $ref Node renders once as next: Node rather than expanding forever. Plain objects without an explicit type still emit interfaces when they carry properties or $defs, while additionalProperties: true yields [key: string]: unknown and an object schema value yields the resolved value type.

    Validation runs before emission and fails loudly: non-object input throws Schema must be a JSON object, compileJsonSchemaJson throws Schema is not valid JSON on unparseable text, unknown type strings throw Invalid schema: unsupported type at <path>, and malformed properties, required, anyOf/oneOf, or $defs entries report their JSON-pointer path. Identifier sanitizing strips non-identifier characters and prefixes digit-leading names, so output is deterministic regardless of property insertion order.

    How to use this tool

    1. Paste a schema

      Supply a JSON object schema with type, properties, required, and optionally $defs. Click compile or call compileJsonSchemaToTypescript(schema) to get the Root interface plus one interface per definition.

    2. Model unions and maps

      Use anyOf/oneOf for unions, enum for literal unions, type arrays for nullable shapes, and additionalProperties for string-keyed maps that render as [key: string]: T.

    3. Wire up $refs

      Point $ref at #/$defs/Name, #/definitions/Name, or # for the root. Cyclic refs are safe: the target renders once and referrers use its alias name.

    4. Read the errors

      Unsupported types, non-object properties, and bad required/anyOf shapes throw with a path (e.g. $.address). Fix the reported node instead of guessing.

    Use cases

    Typing API responses

    Turn an endpoint's JSON Schema into Root plus named definition interfaces for request/response payloads.

    Modeling recursive data

    Express trees and linked structures with self-referencing $defs entries that compile to alias references instead of infinite nesting.

    Documenting config shapes

    Convert hand-written schemas with descriptions, enums, and string maps into commented interfaces for config files.

    Common mistakes

    Mistake:Expecting optional fields to be required.

    Fix:Only keys listed in required lose the ?. Add the key to required when the field must always be present.

    Mistake:Passing a JSON string to compileJsonSchemaToTypescript.

    Fix:That function takes an object; pass raw text to compileJsonSchemaJson instead, which parses then compiles and throws Schema is not valid JSON on bad input.

    Mistake:Using an unsupported type name.

    Fix:Stick to array, boolean, integer, null, number, object, string. Anything else throws Invalid schema: unsupported type at the offending path.

    Frequently asked questions

    What does this tool do?

    It compiles a JSON Schema object into TypeScript interfaces with compileJsonSchemaToTypescript (raw text via compileJsonSchemaJson): sorted properties, required keys without ? and optional keys with ?, anyOf/oneOf/enum/const unions, arrays, $ref aliases from $defs/definitions, and additionalProperties index signatures.

    How do cyclic $refs, unions, and maps render?

    A self-referencing $defs/Node entry emits once with next: Node instead of recursing; anyOf string|number renders value?: string | number; additionalProperties string renders [key: string]: string; and keys absent from required always gain ? (e.g. nickname?: string alongside id: number).

    Is my schema uploaded or stored?

    No. Compilation, $ref resolution, and validation are pure client-side computation — schemas never leave your browser.

    Why did compilation throw?

    Non-object input throws Schema must be a JSON object, bad JSON throws Schema is not valid JSON, and unknown types or malformed properties/required/anyOf nodes throw Invalid schema errors naming the $ path — fix the reported node.

    References & standards

    .env Secret Safety Linter

    Validate .env syntax and spot likely leaked credentials or placeholder secrets.

    .htaccess Generator

    Build an Apache .htaccess — HTTPS redirects, rewrites, error pages, security headers, and basic auth

    2D Fluid Dynamics & Particle Physics Sandbox

    A physics playground with two engines sharing one canvas. The fluid mode runs a real Eulerian Navier-Stokes solver — semi-Lagrangian advection, explicit diffusion, Jacobi pressure projection and vorticity confinement — that you paint with dye and velocity using mouse or touch. The particle mode is a damped Verlet N-body engine with gravity wells, Coulomb attractors and repulsors, spring links, wall restitution and elastic circle collisions. Particle counts adapt to the device (100k target on WebGL2 desktop, 2k-5k on CPU fallback), and every configuration exports as runnable code: a Matter.js world, a PixiJS ticker setup, or a standalone vanilla Canvas engine.

    AI Function Calling Schema Generator

    Generate function calling JSON schemas from TypeScript interfaces

    Algorithm Arena & Data Structure Visualizer

    Interactive arena for algorithms and data structures: eight sorting algorithms racing on one seeded input, six pathfinding algorithms walking generated mazes, and BST, AVL, Red-Black, B-tree, heap and trie operations replayed step by step with rotations highlighted. Every run shows the Big-O table and the phase-highlighted reference implementation in four languages, and compared values can be played as pitch (sonification) through the shared audio engine. Everything runs client-side; inputs never leave the browser.

    Ansible Playbook Generator

    Build an Ansible playbook — hosts, vars, and tasks with modules — as YAML