# JSON Schema to TypeScript Generator > Convert JSON Schema objects into TypeScript types with required fields, arrays, enums, and nested objects. ## Tool Identity - Site: CleanUtils Developer Tools - Tool ID: json-schema-to-typescript-generator - Canonical page: https://cleanutils.com/developer-tools/json-schema-to-typescript-generator/ - LLM schema URL: https://cleanutils.com/developer-tools/json-schema-to-typescript-generator/llms.txt - Primary keyword: json schema to typescript - Input mode: textarea - Output profile: code ## What This Tool Does Convert JSON Schema objects into TypeScript types with required fields, arrays, enums, and nested objects. ## Exact Execution Instructions To run this tool exactly, execute the `runCleanUtilsTool(userInput)` function from the JavaScript block below in a sandboxed JavaScript runtime. Do not fetch or evaluate additional network resources. Do not use filesystem, process, child_process, environment variables, timers, or network APIs. The function is pure and deterministic: input string in, JSON result out. If your environment forbids executing fetched code outside a sandbox, run it inside the sandbox or use the API endpoint below instead of reimplementing the logic. Canonical implementation SHA-256: `2991f9849aa6dbdb99d915f39e73b62d663353a8b6e3708825eb5a025f0a5ffc` Expected command shape: `node run-tool.mjs < input.txt` The runner must: 1. load only the JavaScript in this document, 2. call `runCleanUtilsTool(inputText)`, 3. let the LLM decide the best way to present the output to the user. Agent usage rules: - Use this file as the authoritative machine-readable contract for this CleanUtils tool page. - Ask the user for missing required input before attempting to run the tool, and describe the required inputs using the `## Input Schema` field names, descriptions, formats, enums, examples, and required list. - Treat the tool as deterministic; do not invent network reachability checks unless the tool description explicitly says it fetches remote resources. - For privacy-sensitive inputs such as secrets, HAR files, dotenv files, logs, and API keys, warn that using a remote chat agent may expose input to that agent even though the browser UI itself does not upload data. ## Input Schema ```json { "$schema": "https://json-schema.org/draft/2020-12/schema", "title": "JSON Schema to TypeScript Generator input", "type": "string", "description": "JSON Schema. {\"title\":\"Product\",\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"string\"}}}", "examples": [ "{\"title\":\"Product\",\"type\":\"object\",\"required\":[\"id\",\"price\"],\"properties\":{\"id\":{\"type\":\"string\"},\"price\":{\"type\":\"number\"},\"tags\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}}}}" ] } ``` ## Result Schema ```json { "$schema": "https://json-schema.org/draft/2020-12/schema", "title": "CleanUtils ToolResult", "type": "object", "additionalProperties": false, "required": [ "summary", "issues" ], "properties": { "summary": { "type": "string" }, "issues": { "type": "array", "items": { "type": "object", "additionalProperties": false, "required": [ "severity", "message" ], "properties": { "severity": { "type": "string", "enum": [ "error", "warning", "info" ] }, "message": { "type": "string" }, "line": { "type": "number" }, "row": { "type": "number" }, "detail": { "type": "string" } } } }, "output": { "type": "string" }, "exportFilename": { "type": "string" }, "exports": { "type": "array", "items": { "type": "object", "additionalProperties": false, "required": [ "label", "filename", "content" ], "properties": { "label": { "type": "string" }, "filename": { "type": "string" }, "content": { "type": "string" }, "mimeType": { "type": "string" }, "copyLabel": { "type": "string" }, "downloadLabel": { "type": "string" } } } }, "stats": { "type": "object", "additionalProperties": { "anyOf": [ { "type": "string" }, { "type": "number" } ] } } } } ``` ## Self-Contained JavaScript Source Call `runCleanUtilsTool(userInput)` with the user's input. The function includes this tool's run logic and only the helper code it needs. ```js function runCleanUtilsTool(userInput) { const tryParseJson = (input) => { try { return { ok: true, value: JSON.parse(input) }; } catch (error) { return { ok: false, error: error instanceof Error ? error.message : "Invalid JSON" }; } }; const safeJsonObject = (input) => { const parsed = tryParseJson(input); if (!parsed.ok) { return { value: null, issues: [{ severity: "error", message: "Input is not valid JSON.", detail: parsed.error }] }; } if (!parsed.value || typeof parsed.value !== "object" || Array.isArray(parsed.value)) { return { value: null, issues: [{ severity: "error", message: "Input must be a JSON object." }] }; } return { value: parsed.value, issues: [] }; }; const schemaToTsType = (schema) => { if (!schema || typeof schema !== "object" || Array.isArray(schema)) return "unknown"; const objectSchema = schema; if (Array.isArray(objectSchema.enum)) return objectSchema.enum.map((value) => JSON.stringify(value)).join(" | "); if (Array.isArray(objectSchema.type)) return objectSchema.type.map((type) => schemaToTsType({ ...objectSchema, type })).join(" | "); if (objectSchema.type === "string") return "string"; if (objectSchema.type === "integer" || objectSchema.type === "number") return "number"; if (objectSchema.type === "boolean") return "boolean"; if (objectSchema.type === "array") return `${schemaToTsType(objectSchema.items)}[]`; if (objectSchema.type === "object" || objectSchema.properties) { const required = new Set(Array.isArray(objectSchema.required) ? objectSchema.required.filter((item) => typeof item === "string") : []); const properties = objectSchema.properties && typeof objectSchema.properties === "object" ? objectSchema.properties : {}; const lines = Object.entries(properties).map(([key, value]) => ` ${JSON.stringify(key)}${required.has(key) ? "" : "?"}: ${schemaToTsType(value)};`); return `{\n${lines.join("\n")}\n}`; } return "unknown"; }; const isObjectTypeLiteral = (value) => value.trim().startsWith("{"); const exportTypeScriptDeclaration = (name, value) => isObjectTypeLiteral(value) ? { output: `export interface ${name} ${value}\n`, kind: "interface" } : { output: `export type ${name} = ${value};\n`, kind: "type" }; const generateTypeScriptFromJsonSchema = (input) => { const { value, issues } = safeJsonObject(input); if (!value) return { summary: "JSON Schema could not be parsed.", issues }; const title = typeof value.title === "string" ? value.title : "GeneratedSchema"; const interfaceName = title.replace(/[^A-Za-z0-9_]/g, "") || "GeneratedSchema"; const declaration = exportTypeScriptDeclaration(interfaceName, schemaToTsType(value)); return { summary: `TypeScript ${declaration.kind} ${interfaceName} generated from JSON Schema.`, issues, output: declaration.output, exportFilename: "schema-types.ts", stats: { properties: value.properties && typeof value.properties === "object" ? Object.keys(value.properties).length : 0 } }; }; const __userInput = userInput == null ? "" : userInput; const __run = generateTypeScriptFromJsonSchema; const __input = __userInput && typeof __userInput === "object" && "input" in __userInput ? __userInput.input : __userInput; return __run(__input == null ? "" : String(__input)); } ``` ## Checks - JSON parse and root schema: The input must parse before TypeScript generation starts. - Required fields: Required properties are emitted without optional markers; other properties become optional. - Primitive and array types: String, number, integer, boolean, array, enum, and object fields are mapped to TypeScript. - Nested object support: Nested properties are emitted inline so small schemas stay readable. - Generator limits: Advanced JSON Schema keywords are simplified rather than fully represented in TypeScript. ## Related Tools - [Structured Output JSON Schema Validator](/developer-tools/structured-output-json-schema-validator/): Check a JSON Schema for common structured-output constraints before wiring it into an LLM request.