JSON to TypeScript
Convert a JSON object to TypeScript interfaces. Handles nested objects, arrays, optional properties, and union types.
How to Use JSON to TypeScript
- 1Paste your JSON object or API response.
- 2Enter a name for the root interface.
- 3Click Convert to generate TypeScript interfaces.
- 4Copy the generated TypeScript code.
Zenovay
Privacy-first analytics for your website
Understand your visitors without invasive tracking. GDPR compliant, lightweight, and powerful.
Related Tools
JSON Formatter & ValidatorFormat, validate, and beautify JSON data with syntax highlighting and error detection.
JWT DecoderDecode and inspect JWT tokens. View header, payload, and verify signatures.
Base64 Encode/DecodeEncode text to Base64 or decode Base64 back to text. Supports UTF-8 and binary data.
URL Encode/DecodeEncode or decode URL components. Handle special characters, query strings, and full URLs.
Frequently Asked Questions
What TypeScript features does this generate?▾
The converter generates TypeScript interface declarations for objects, with correct types for string, number, boolean, null, undefined, and Date (ISO date strings). Nested objects produce separate interfaces. Arrays produce T[] types. Mixed arrays produce union types (string | number)[]. Properties that are null in the sample get T | null. Optional properties (?) are not inferred from a single sample — use multiple samples for better inference.
How are nested objects handled?▾
Each nested object gets its own named interface. For example, {"user": {"name": "Alice"}} produces interface User { name: string; } and interface Root { user: User; }. Interfaces are named by PascalCase-converting the property key. Deeply nested objects produce a full hierarchy of interfaces. Arrays of objects produce an interface for the item type, referenced as ItemType[].
What is the difference between interface and type in TypeScript?▾
interface and type are largely interchangeable for object shapes. Interfaces support declaration merging (multiple declarations with the same name merge) and can be extended with extends. type aliases can express union types, tuples, and mapped types that interfaces cannot. For API response shapes, both work. The convention is to use interface for object shapes (objects you will instantiate or extend) and type for aliases, unions, and computed types.
How should I handle API responses that vary between calls?▾
The converter infers types from one sample. If a field is sometimes string and sometimes number across calls, you need to add the union type manually: field: string | number. For nullable fields, add | null. For missing/optional fields, add the ? modifier: field?: string. Run the converter on multiple API responses and merge the interfaces manually for production-quality types.
How do I use these interfaces in my TypeScript project?▾
Copy the generated interfaces into a .ts or .d.ts file (e.g., api-types.ts). Import them where needed: import type { Root } from "./api-types";. Type your fetch response: const data = await response.json() as Root; or with zod for runtime validation: const schema = z.object({...}); const data = schema.parse(await response.json()). For large projects, consider tools like quicktype or openapi-typescript for automated type generation from schemas.