# JSON to TypeScript Converter > Infer a TypeScript type from sample JSON, including nested objects, arrays, booleans, numbers, and nulls. ## Tool Identity - Site: CleanUtils Developer Tools - Tool ID: json-to-typescript-converter - Canonical page: https://cleanutils.com/developer-tools/json-to-typescript-converter/ - LLM schema URL: https://cleanutils.com/developer-tools/json-to-typescript-converter/llms.txt - Primary keyword: json to typescript - Input mode: textarea - Output profile: code ## What This Tool Does Infer a TypeScript type from sample JSON, including nested objects, arrays, booleans, numbers, and nulls. ## 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: `89c05500c95501b7a1d92601ac86e6b9e35cddad5b0984cda2821ce8c381ff4e` 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 to TypeScript Converter input", "type": "string", "description": "Sample JSON. {\"id\":\"sku_123\",\"price\":19.99}", "examples": [ "{\"id\":\"sku_123\",\"price\":19.99,\"active\":true,\"tags\":[\"feed\",\"sale\"],\"dimensions\":{\"width\":10,\"height\":4}}" ] } ``` ## 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 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 inferTsType = (value) => { if (value === null) return "null"; if (Array.isArray(value)) { const types = [...new Set(value.map(inferTsType))]; return `${types.length ? types.join(" | ") : "unknown"}[]`; } if (typeof value === "object") { const lines = Object.entries(value).map(([key, item]) => ` ${JSON.stringify(key)}: ${inferTsType(item)};`); return `{\n${lines.join("\n")}\n}`; } return typeof value; }; const convertJsonToTypeScript = (input) => { const parsed = tryParseJson(input); if (!parsed.ok) return { summary: "JSON sample could not be parsed.", issues: [{ severity: "error", message: "JSON sample could not be parsed.", detail: parsed.error }] }; const declaration = exportTypeScriptDeclaration("GeneratedFromJson", inferTsType(parsed.value)); return { summary: `TypeScript ${declaration.kind} generated from sample JSON.`, issues: [], output: declaration.output, exportFilename: "json-types.ts", stats: { rootType: Array.isArray(parsed.value) ? "array" : typeof parsed.value } }; }; const __userInput = userInput == null ? "" : userInput; const __run = convertJsonToTypeScript; const __input = __userInput && typeof __userInput === "object" && "input" in __userInput ? __userInput.input : __userInput; return __run(__input == null ? "" : String(__input)); } ``` ## Checks - Valid JSON sample: The converter starts only after the pasted JSON parses successfully. - Primitive inference: String, number, boolean, null, array, and object values are mapped to TypeScript. - Nested objects: Nested structures are emitted inline so the generated type matches the sample shape. - Array unions: Arrays with mixed item shapes produce a union of inferred item types. - Sample limitation: Only the pasted sample is inferred; absent fields and alternate API responses cannot be guessed. ## 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.