How to convert JSON to TypeScript
- Paste your JSON into JSON input, or click Paste. The page opens with a sample user record.
- Set Root name for the top-level type (the default is Root) and choose Output: interface or type alias. Tick Make all properties optional, Readonly properties or Add export if you need them.
- The result appears in TypeScript output as you type. Press Convert or Ctrl+Enter to refresh it, then Copy.
How the types are inferred
The tool reads every value once and builds a type from what it sees, following the TypeScript handbook types for strings, numbers, booleans, null, arrays and objects. Each nested object becomes its own declaration named after its key in PascalCase, so company gives Company, and an array of objects under orders gives Order.
All items of an array are merged into one type. A field missing from some items becomes optional, and a field with different value types becomes a union. If two different shapes share a name, the second takes its parent's name (CompanyAddress); identical shapes under one name share a single declaration. Empty arrays become unknown[] and empty objects Record<string, unknown>.
Worked example
The sample JSON is a user with an address, a company that has its own address, a tags array, and two orders, one with a coupon of null and one with an extra gift field. With the default settings the tool shows:
interface Root {
id: number;
name: string;
email: string;
active: boolean;
address: Address;
company: Company;
tags: string[];
orders: Order[];
}
interface Address {
street: string;
city: string;
}
interface Company {
name: string;
address: CompanyAddress;
}
interface CompanyAddress {
city: string;
zip: string;
}
interface Order {
id: number;
total: number;
coupon: string | null;
gift?: boolean;
}
Note coupon: string | null and gift?: boolean: both come from merging the two orders.
Limits and edge cases
- Types describe your sample only. A field present in every sample item can still be missing in other responses, so use realistic JSON or tick Make all properties optional.
- JSON carries no dates, enums or integer types, so dates stay
stringand every number isnumber. - An object used as a lookup table, with a different key per entry, produces one property per key. Replace it by hand with
Record<string, T>. - Array items with truly different shapes merge into one interface with optional fields. Split them into a discriminated union yourself.
- Keys that are not valid identifiers, such as
user-name, are quoted. Invalid JSON shows the line and column of the error.
Frequently asked questions
Should I use an interface or a type alias?
For the object shapes this tool generates, both work the same way. Interfaces can be extended and merged, and type aliases can also name unions and intersections. Pick whichever your project already uses; you can switch with the Output list and compare.
How are arrays of objects with different fields handled?
Every item is merged into one type. A field that appears in only some items becomes optional (gift?: boolean), and a field whose value differs in type becomes a union (string | null). Check that your JSON includes a few realistic items so the merge sees the variation.
Why is every number typed as number?
JSON has a single number type, and TypeScript number covers integers and decimals alike. The converter cannot tell an id from a price, so refine fields such as ids or enums by hand after copying.
How can I check the generated types against my data?
In a TypeScript file, paste the output and assign your JSON to the root type, for example const user: Root = { ... }. Running tsc with the strict option then reports missing or unexpected fields.
Related tools: check or tidy your data with the JSON formatter and validator, turn tables into JSON with the CSV to JSON converter, or paste a token payload from the JWT decoder.