JSON to TypeScript
Free JSON to TypeScript converter: generate interfaces or type aliases with optional fields, null unions, nested types and readonly or export. Runs in your browser.
How to use JSON to TypeScript
- 1Paste a representative JSON sample, or press Try example.
- 2Set the root name and choose interface or type alias, readonly and export.
- 3Copy the generated types or download types.ts.
What makes it better
- Interfaces or type aliases, with optional readonly and export
- Merges all items of an array to find optional and nullable fields
- Unions for mixed values, such as (string | number)[]
- Nested objects extracted into named interfaces
- Keys that are not valid identifiers are quoted automatically
About JSON to TypeScript
JSON to TypeScript reads a sample document and writes the types that describe it. Objects inside arrays are merged, so a field that is missing from some items becomes optional, and a value that is sometimes null becomes a union such as string | null. Nested objects get their own named interfaces, and you can switch between interface and type alias, add readonly or leave out export.
Common uses
- Type an API response quickly instead of writing interfaces by hand.
- Describe the shape of a JSON config or fixture for a TypeScript project.
- Start a data model from a sample, then refine it manually.
- Compare how a sample maps to types before writing a validation schema.
Example
Input
{"id":101,"name":"Ada Lovelace","email":"[email protected]","active":true,"tags":["math","code"],"address":{"city":"London","zip":null},"orders":[{"sku":"A-1","qty":2},{"sku":"B-7","qty":1,"gift":true}]}Result
export interface Root {
id: number;
name: string;
email: string;
active: boolean;
tags: string[];
address: Address;
orders: Order[];
}
export interface Address {
city: string;
zip: null;
}
export interface Order {
sku: string;
qty: number;
gift?: boolean;
}Good to know
- Types are inferred from the sample only. A field that never appears in your sample, or is always null, cannot be typed accurately, so use a sample that shows all variants.
- All JSON numbers become number. A value that is always null becomes null, and an empty array becomes unknown[].
- The generated code is a starting point: review names and consider validating data at runtime, since types do not check what the server really sends.
JSON to TypeScript FAQ
How do I convert JSON to a TypeScript interface?
Paste a sample of your JSON and the interface is generated as you type. Nested objects are pulled out into their own named interfaces and the top-level one uses the root name you set, Root by default.
How does it decide which fields are optional?
Every object inside an array is merged into one shape. A field found in only some of the items is marked with a question mark, and a field that is null in some items gets a union with null.
Interface or type alias, which should I use?
Both describe object shapes. Interfaces can be extended and merged, which suits public models, while type aliases can also name unions and tuples. The tool offers either, so match your project's style.
What happens with keys like "first-name"?
Keys that are not valid identifiers are wrapped in quotes in the generated type, for example "first-name": string, so the code compiles and the property still matches the JSON.
Can it generate types for an array at the top level?
Yes. A root array produces an alias such as export type Root = RootItem[] and an interface for the items, with fields merged across all of them.