JSON to TypeScript
What is a Json To Types?
TypeScript catches mistakes before they reach production, but only if your data is typed. When you call an API, you need interfaces that describe the response. Writing them by hand for large payloads is tedious. This tool generates TypeScript interfaces from a sample JSON payload, recursively handling nested objects and arrays.
How to use it
- Paste a representative JSON response.
- Copy the generated interfaces into your project.
- Rename the root interface and refine types the sample could not reveal.
Example
Input (double quotes in real JSON):
{ 'id': 7, 'name': 'Asha', 'email': null, 'roles': ['admin'], 'profile': { 'city': 'Pune', 'age': 31 } }Output:
interface Root {
id: number;
name: string;
email: null;
roles: string[];
profile: Profile;
}
interface Profile {
city: string;
age: number;
}Refine after generating
- Null values: a field that is null in the sample is typed as
null. Change it to the real type plus null, such asemail: string | null. - Optional fields: mark fields that are not always present with
?. - Union and literal types: a status that can only be
'active'or'banned'is better typed as a union than asstring. - Empty arrays give no item type; fill it in.
- Dates arrive as strings in JSON; keep them as
stringuntil you parse them.
Types are not validation
Interfaces disappear at runtime, so a server can still send unexpected data. For untrusted input, validate with a schema library such as Zod or Valibot and infer the TypeScript type from the schema.
Frequently Asked Questions
Why is a field typed as null?
The sample contained null, so the real type is unknown. Change it to something like string | null.
Should I use interface or type?
Both work for object shapes. Interfaces can be extended and merged, while type aliases also support unions and other advanced types.
Do TypeScript interfaces validate API data?
No. Types are removed at runtime. Use a validation library such as Zod for untrusted data.
How do I mark optional fields?
Add a question mark after the property name, for example nickname?: string.