JSON to TypeScript
Generate TypeScript interfaces from JSON with nested types and optional fields
Shortcuts: Ctrl+Enter run · Ctrl+L clear · Ctrl+D download
About JSON to TypeScript
This tool generates TypeScript interfaces directly from a JSON sample, saving the tedious work of writing type definitions by hand. Paste any JSON object or array and get a complete, nested TypeScript interface in seconds. It detects optional fields, handles arrays of objects, and produces clean, idiomatic TypeScript that you can drop straight into your project.
- ✓Generates nested interfaces for deeply nested JSON structures
- ✓Detects optional fields when values are null or inconsistent across array items
- ✓Produces readable interface names derived from JSON key names
- ✓Handles arrays, union types, and mixed-type values automatically
From JSON Sample to Usable TypeScript
Writing TypeScript types by hand for a large API response is tedious and error-prone — it is easy to mistype a field name or miss a nested object. Generating the interface directly from a real JSON sample guarantees the type matches the actual shape of the data, at least for the sample you provide.
What a generated interface looks like
// Input JSON
{
"id": 42,
"name": "Alice",
"email": null,
"roles": ["admin", "editor"],
"address": { "city": "Berlin", "zip": "10115" }
}
// Generated TypeScript
interface Address {
city: string
zip: string
}
interface Root {
id: number
name: string
email: string | null
roles: string[]
address: Address
}Notice that nested objects become their own named interfaces rather than being inlined — this keeps the output readable and lets you reuse the nested type elsewhere (e.g., `Address` could be reused for a `shippingAddress` field that has the same shape).
Why a generated type is a starting point, not a finished contract
- ·A type generated from one sample reflects only what that sample contains — if a field is sometimes present and sometimes missing, but your sample happened to include it, the generated type will incorrectly mark it as required
- ·Numeric fields that happen to be whole numbers in the sample (e.g., `"price": 10`) generate `number`, which is correct — but fields that are sometimes integers and sometimes decimals will only show their sampled precision
- ·Enums and string literal unions are not inferred — a `"status": "active"` field becomes `status: string`, even if the API only ever returns a small fixed set of values
Tip: Treat the generated interface as a first draft. Cross-reference it with API documentation (if available) to mark genuinely optional fields with `?`, narrow loose `string` fields to literal unions like `"active" | "inactive"`, and add JSDoc comments for fields whose meaning is not obvious from the name.
Interfaces vs. type aliases vs. runtime validation
- ·TypeScript interfaces and type aliases are compile-time only — they vanish when your code is compiled to JavaScript and provide zero protection against malformed data arriving at runtime
- ·If you need to guarantee that data matches a shape at runtime (e.g., validating an API response before trusting it), pair your generated types with a runtime validation library such as Zod, io-ts, or ajv with a JSON Schema
- ·A common pattern: generate the TypeScript interface here for editor autocompletion and compile-time checks, and generate a JSON Schema with the JSON Type Generator tool for runtime validation of the same shape