๐ท JSON to TypeScript
interface RootObject {
id: number;
name: string;
active: boolean;
tags: string[];
address: {
city: string;
zip: string;
};
}Paste in JSON data and this tool automatically generates the matching TypeScript interface definition. Supports nested objects and arrays. Handy for quickly creating a type for an API response, or generating a type definition from existing JSON data.
How to use
- Enter a name for the generated interface (optional).
- Paste the JSON data you want a type definition for into the input field.
- The TypeScript interface is generated automatically โ use "Copy" to copy it.
How the calculation works
This tool generates TypeScript type definitions (interfaces) from a JSON example, saving you from writing API response types by hand. Types are chosen per value: โข string โ string, number โ number, true/false โ boolean, null โ null โข object โ a nested type listing each key's type โข array โ the element type followed by []; mixed element types become a union such as (A | B)[], and an empty array becomes unknown[] Keys that are not valid JavaScript identifiers, such as "first-name", are quoted. If the top level is an object the output is an interface; otherwise (an array, for example) it is a type alias.
Worked example
Input {"id": 1, "name": "Alice", "active": true, "tags": ["admin", "user"], "address": {"city": "Tokyo", "zip": "100-0001"}} Output interface RootObject { id: number; name: string; active: boolean; tags: string[]; address: { city: string; zip: string; }; }
Things to be aware of
- Types are inferred from this one example. Keys that do not appear, or values that can sometimes be null, are not reflected; add "?" (optional) or "| null" as needed.
- Nested objects are written inline rather than as separate interfaces. Split them out and name them by hand if you want to reuse them.
- Date strings are typed as string. Convert them after receiving the data if you want Date objects.
FAQ
What happens if an array's elements have mixed types?
If an array contains multiple different value types, a union type is generated (e.g. (string | number)[]).
Is the generated type exactly correct?
It's a basic type inferred from the JSON values. In a real API, the same field might sometimes be null or undefined, so adjust fields to be optional (?) as needed.
Are dates or other special string formats detected automatically?
No, date strings (like ISO 8601 format) are also treated as a regular string type.