json-to-typescript
A JSON-to-TypeScript converter analyzes a JSON sample (typically an API response) and emits matching TypeScript interface definitions, automatically inferring types for primitives, arrays, nested objects, and union types from sibling examples. The ZTools JSON-to-TypeScript tool handles deeply nested structures, optional fields (when keys appear in some objects but not others within an array), and union types (when array elements have different shapes), producing clean, idiomatic TypeScript code ready to paste into a .d.ts or .ts file.
Use casesβ
Adding type safety to a third-party API integrationβ
You're calling a REST API that has no TypeScript SDK. Paste a sample response, generate interfaces, and your code is now type-safe β no more response: any and runtime KeyError surprises. Saves an hour of manual interface authoring per endpoint.
Generating types from a JSON schema or fixture fileβ
Your test fixtures are JSON files. Convert them to TypeScript interfaces, then your test code can import the same types your production code uses β single source of truth for the data shape.
Migrating a JavaScript codebase to TypeScriptβ
During incremental migration, convert sample data shapes to TypeScript interfaces and use them to type the previously-untyped JS code. Dramatically reduces the manual typing effort during conversion.
Documenting a data structure for new team membersβ
A TypeScript interface is the most precise documentation for a data shape. Generate from a sample, drop into your docs or types module, and new developers see the entire structure at a glance.
How it worksβ
- Paste a JSON sample into the input pane β Single object or array of objects. The tool walks the structure recursively to infer all nested types.
- Configure naming β Root interface name (default
RootObject). Nested interfaces are named after their parent key (e.g.,addressβAddressinterface). - Click Generate β The tool builds an AST of the JSON, infers a TypeScript type for every leaf and node, and emits
interfacedeclarations in dependency order. - Review the output β Generated TypeScript appears in the right pane with syntax highlighting. Optional fields are marked with
?; union types appear asstring | number. - Copy or download β Save as
.tsor paste into your project. The output is idiomatic TypeScript ready to be consumed bytscwithout modification.
Examplesβ
Input: { "name": "Ahsan", "age": 32, "tags": ["admin", "user"] }
Output: interface RootObject { name: string; age: number; tags: string[]; }
Input: [{ "id": 1, "label": "a" }, { "id": 2, "label": "b", "extra": true }]
Output: interface RootObject { id: number; label: string; extra?: boolean; }
Frequently asked questionsβ
How does the tool handle null values?
A field that is sometimes null produces T | null. A field that is always null produces just null (and a warning suggests providing a richer sample).
What about union types in arrays?
When array elements have different shapes (e.g., [\{type: "a", x: 1\}, \{type: "b", y: 2\}]), the tool generates a union type and emits both interfaces. Discriminated unions work especially well.
Can I generate types from a JSON Schema directly?
This tool infers types from sample data. For JSON Schema input, use json-schema-to-typescript (npm package) or our JSON Schema Generator tool to produce a schema first.
Are dates auto-detected?
JSON has no native date type β dates appear as strings. The tool defaults to string. If you want stricter typing, manually replace with Date or a branded type after generation.
Does the output use interface or type?
Default is interface (better for object shapes, supports declaration merging). A toggle switches to type aliases if you prefer.
Will the generated types catch every runtime mismatch?
They're inferred from one sample, so edge cases not in your sample (different shapes, unexpected nulls) won't be caught. Always validate at the boundary with zod or io-ts for production code.
Tipsβ
- Provide a representative sample β include both the common case and an edge case (e.g., a record with optional fields populated).
- After generating, replace
stringwithDate, branded types, or enums where the JSONstringactually represents a richer type. - For runtime validation, pair generated types with a
zodschema β types alone don't protect against malformed input at runtime. - Re-generate when the API changes; diff the new output against the old to spot breaking changes.
Try it nowβ
The full json-to-typescript runs in your browser at https://ztools.zaions.com/json-to-typescript β no signup, no upload, no data leaves your device.
Last updated: 2026-05-05 Β· Author: Ahsan Mahmood Β· Edit this page on GitHub