JSON to TypeScript Interface
Paste a JSON sample and get TypeScript interfaces, with nested objects, arrays, optional fields and union types worked out for you.
Paste a JSON sample and get TypeScript interfaces, with nested objects, arrays, optional fields and union types worked out for you.
Writing TypeScript types for an API response by hand is slow. Paste a sample of the JSON and this tool infers the interfaces for you.
Nested objects become their own interfaces, arrays of objects are merged into one, fields that are missing in some items become optional, and mixed values become union types.
export interface User {
id: number;
name: string;
address: Address;
}
export interface Address {
city: string;
}
The converter infers types from one sample by walking the JSON tree. Strings, numbers and booleans map to string, number and boolean, null maps to null, and objects become interfaces named after their property, for example an address property becomes an Address interface. For arrays, every element is inspected and the object shapes are merged. A property present in only some elements becomes optional, written with a question mark, and a property with different value types becomes a union such as string | number.
Inference from a sample has real limits. An empty array gives no information about its element type, so it cannot be typed precisely. A field that is always a string in your sample may be null in production, and a number field cannot show whether values are integers or floats because TypeScript has only number. Interfaces also exist only at compile time and do not validate data at runtime, so pair them with a runtime check, such as a schema library, when handling untrusted input.
From the values in your sample. A sample cannot show every possible value, so review the result, especially for null and optional fields.
All objects in an array are merged into one interface. A property that is missing from some of them is marked optional.
They are quoted, for example "first-name": string.
No. It stays in your browser.
No. It outputs plain TypeScript interfaces only. They disappear on compilation, so they do not check data at runtime. Use a validation library if you need that.
The tool produces interfaces. You can change them to type aliases by hand, and string unions or enums are not inferred automatically because a sample cannot show every allowed value.