Paste JSON and get TypeScript interfaces for it as you type.
Paste JSON, upload a .json file or drop one here.
Updates as you type. Copy it or download types.ts.
Converting…
This free JSON to TypeScript converter reads a JSON document and writes the TypeScript interfaces that describe it. Paste an API response, a config file or a fixture, and you get a root interface plus a named interface for every nested object, with arrays typed, optional properties marked and differing value types combined into unions. It saves you from typing out types for a large response by hand, and from the typos that come with it.
Everything runs in your browser. Your JSON is not uploaded, so you can paste a real response without worrying about what it contains. There is nothing to install and no account to create. The output is plain interface or type declarations with no runtime code, ready to drop into a types.ts file.
.json file. You can also drag a file onto the editor.User or ApiResponse. Spaces and punctuation are cleaned up so the name is always a valid identifier.interface or a type alias, and tick readonly properties if the data should not be modified.types.ts.The root object becomes User, using the name typed in the options. The nested address object becomes Address, and the objects inside the orders array become one Order interface. Because only the second order has a coupon, coupon is optional.
{
"id": 1,
"name": "Ada",
"address": { "city": "London" },
"orders": [
{ "id": "A-1", "total": 9.5 },
{ "id": "A-2", "total": 12, "coupon": "SPRING" }
]
}export interface User {
id: number;
name: string;
address: Address;
orders: Order[];
}
export interface Address {
city: string;
}
export interface Order {
id: string;
total: number;
coupon?: string;
}The converter looks at the values in your sample and picks the TypeScript type that fits every one it sees. It is only as informed as the sample, which is why a few cases need your attention.
| JSON value | TypeScript type | Notes |
|---|---|---|
| string | string | ISO 8601 timestamps stay string, as JSON has no date type. |
| number | number | Integers and decimals are both number. |
| true / false | boolean | |
| null | null | Becomes a union such as string | null when other items hold a value. |
| object | A named interface | Nested objects each get their own interface. |
| array | T[] | Mixed contents become Array<number | string>. |
| empty array | unknown[] | There are no items to learn the type from. |
A single object has exactly one value for each key, so every property looks required. To get coupon?: string, give the converter several examples in an array. A property that is missing from some items is marked optional, and a property whose type differs between items becomes a union. If you start from one response, paste two or three different ones inside [ ].
A top-level array of objects produces one interface for the items, not an array type, so [{ ... }, { ... }] gives you Root describing a single element. Use Root[] where you need the list.
A nested object is named after the property that holds it, in PascalCase: address becomes Address. For arrays the name is singular, so orders becomes Order. When two different shapes would end up with the same name, the second gets a prefix from its parent, such as AItem and BItem.
Objects with exactly the same shape are merged into one interface. If billing and shipping both contain just a street, they will share a single type, and the name it gets may not be the one you would choose. Rename it in your editor; that is a quick find-and-replace.
A property that is null in your only sample becomes null, which is rarely what you mean. Replace it with the real type, for example Manager | null, or add a second sample where the value is present.
If an object uses IDs or names as its keys, such as { "u1": {...}, "u2": {...} }, the converter writes one property per key, because it cannot know the keys are arbitrary. For a real dictionary, replace that interface with Record<string, User> by hand.
Both describe the same object shapes. Interfaces can be extended and merged with extends, while type aliases can also name unions and tuples. Pick whichever your codebase already uses. The readonly properties option adds readonly to every property; it does not change the types of arrays, which stay mutable.
Save the output as types.ts and import the interfaces where you handle the data. With fetch you assert the type of the parsed body, and with axios you pass it as a generic.
import type { User } from "./types";
// fetch: assert the type of the parsed body
const res = await fetch("/api/users/1");
const user = (await res.json()) as User;
// axios: pass the type as a generic
const { data } = await axios.get<User>("/api/users/1");Keep in mind that both are only assertions: TypeScript does not check that the response really has that shape. When the data comes from an API you do not control, validate it at runtime with a library such as Zod. Write the schema using the generated interface as your reference, and annotate it with z.ZodType<User> so the compiler tells you if the two ever disagree.
import { z } from "zod";
import type { User } from "./types";
// The annotation fails to compile if the schema drifts from the interface
const UserSchema: z.ZodType<User> = z.object({
id: z.number(),
name: z.string(),
address: z.object({ city: z.string() }),
orders: z.array(
z.object({ id: z.string(), total: z.number(), coupon: z.string().optional() })
),
});
const user = UserSchema.parse(await res.json()); // throws if the data does not matchIf you only have a description of the data rather than a sample, generate a schema first with the JSON Schema Generator, or create realistic sample data with the JSON Generator and convert that. Working in another language? Try the JSON to C# class converter, the JSON to Go struct converter or the JSON to Java class converter. Comparing formats for typed contracts between services? Read Protobuf vs JSON.
Paste your JSON into the JSON Input editor, or upload a .json file. The TypeScript interfaces appear in the output editor as you type. Copy them, or click Download to save types.ts.
A single object cannot show which fields are optional, because it only has one value for each. Wrap several examples in an array and the converter marks any property that is missing from some of the items with a question mark, such as coupon?: string.
Yes. Every nested object gets its own named interface, named after its property: an address object becomes Address, and items in an orders array become Order. Objects with identical shapes share one interface.
For plain object shapes they behave almost the same. Interfaces can be extended and merged, while type aliases can also describe unions and tuples. Pick one in the Declare as menu and the whole output follows it.
A sample that contains only null tells the converter nothing about the real type, so it writes null. If the same property is a string in another array item, the result is a union such as null | string. Replace a lone null with the type you expect, for example Manager | null.
No. TypeScript types are erased when your code is compiled, so they describe data but do not validate it. To check API responses at runtime, write a schema with a library such as Zod, using the generated interface as your guide.
No. The types are generated in your browser, so your JSON never leaves your device. That makes it safe to use with real API responses that contain private data.