JSON to TypeScript Interface Converter
Paste a JSON sample and get TypeScript interfaces. Nested objects get their own interfaces, mixed arrays become union types, and the output updates as you type.
// TypeScript interfaces will appear here
Related Developer Tools
Runs in your browser: your files and text are not uploaded. Free, no account needed.
Last updated: · Reviewed by the QuickTooly Team
How types are inferred
Type Mapping Rules
- Primitives:
string,number(integers and decimals alike) andboolean. null: the literal typenull. Widen it by hand, for example tostring | null.- Nested objects: a new interface named after its parent and key, for example
RootAddress. - Arrays of objects: one
...Iteminterface built from the keys of every object in the array. A key missing from some items gets?. - Mixed arrays: a union of the element types, such as
(string | number)[]. An empty array becomesunknown[]. If an array holds objects and other values, the other values are ignored and the type is the item interface array.
Worked example
Input:
{"id":7,"score":9.5,"nickname":null,"tags":["a",1],
"address":{"city":"Oslo"},
"orders":[{"sku":"A1","qty":2},{"sku":"B2"}]}Output with default settings:
interface RootAddress {
city: string;
}
interface RootOrdersItem {
sku: string;
qty?: number;
}
interface Root {
id: number;
score: number;
nickname: null;
tags: (string | number)[];
address: RootAddress;
orders: RootOrdersItem[];
}Options
- All fields optional (off by default): adds
?to every field. Useful for partial update payloads. - Readonly (off by default): adds
readonlyto every field. - Top-level arrays: an array of objects gives an item interface plus
type Root = RootItem[]. An array of plain values gives an interface with a singleitemsfield.
Things to Fix by Hand
- Interfaces are not exported. Add
exportif you put them in a module. - Keys that are not valid identifiers, such as
first-name, are written in double quotes:"first-name": string. If such a key holds an object, its interface name drops the invalid characters, sofirst-nameunderRootbecomesRootFirstName. Two keys that differ only in those characters get the same interface name, so rename one of them. - Inside an array of objects, a key's type comes from the first object that has it.
[{"a":null},{"a":"x"}]givesa: null, notstring | null.
JSON to TypeScript Questions
Does it validate data at runtime?
No. The output is type declarations only. They disappear when TypeScript compiles, so check untrusted API data with a runtime validator as well.
What happens with a single number or string?
A top-level value that is not an object or array, such as a single number or string, becomes a type alias such as type Root = number;. Invalid JSON shows an error message instead.
Can I get type aliases instead of interfaces?
The tool writes interface declarations. Change interface Name { to type Name = { by hand if your codebase prefers type aliases.
Related Tools
- JSON to Go: generate matching structs for a Go backend that serves the same JSON.
- JSON Compare: compare two API responses to see which fields changed before you update the types.