JSON to TypeScript
Paste a sample JSON response to generate TypeScript interfaces or a Zod schema. Nested objects get their own named types, and array items are merged so optional fields are detected.
Last updated
JSON
TypeScript
Convertedexport interface User {
id: number;
email: string;
displayName: string;
avatarUrl: null;
roles: string[];
address: Address;
orders: Order[];
createdAt: string;
}
export interface Address {
street: string;
city: string;
postcode: string;
}
export interface Order {
id: string;
total: number;
status: string;
items: Item[];
couponCode?: string;
}
export interface Item {
sku: string;
qty: number;
}
4 declarations
Always null in the sample, so typed as null: avatarUrl. Replace with the real type, e.g. string | null.
Keyboard shortcuts:Ctrl ↵Copy outputCtrl SDownloadCtrl KSearch tools
How to use the JSON to TypeScript
Paste a JSON sample
Use a real API response, config file or fixture. An array of several items gives the best result.
Name the root type
Type a name such as `User` or `OrderResponse`. Nested types are named after their keys.
Choose the output
Pick TypeScript `interface`s, `type` aliases or a Zod schema, and whether to export them.
Copy into your code
Copy or download the `.ts` file, then rename types or tighten fields where you know more than the sample shows.
Features
- TypeScript `interface` or `type` declarations, or a Zod schema with an inferred type
- Nested objects become separate named types, named after their keys
- Array item names are singularised: `orders` gives `Order`, `addresses` gives `Address`
- Objects in the same array are merged; keys missing from some items become optional
- Values that are sometimes null become `T | null` (or `.nullable()` in Zod)
- Mixed arrays become union types, such as `(string | number)[]`
- Identical shapes share one type instead of being duplicated
- Keys that aren't valid identifiers are quoted, and fields that were always null are flagged
- Option to make every property optional, for PATCH payloads
Privacy
Your JSON is processed by JavaScript in your browser tab. Nothing is uploaded or stored, and the page's security policy blocks it from sending data to any other site.
Examples
API response to interfaces
Nested objects get their own interfaces, and the avatar that was null is flagged for you to type.
JSON
{"id":1042,"email":"ada@example.com","avatarUrl":null,"roles":["admin","dev"],"address":{"city":"London","postcode":"N1 9GU"}}TypeScript
export interface User { id: number; email: string; avatarUrl: null; roles: string[]; address: Address; } export interface Address { city: string; postcode: string; }Optional fields from an array
coupon appears in only one order, so it becomes optional; the root is an array type.
JSON
[{"id":"ord_1","total":49.5,"items":[{"sku":"SNS-01","qty":2}]},{"id":"ord_2","total":12,"items":[],"coupon":"WELCOME"}]TypeScript
export type Orders = Order[]; export interface Order { id: string; total: number; items: Item[]; coupon?: string; } export interface Item { sku: string; qty: number; }Zod schema for device telemetry
Validate incoming IoT messages at runtime. battery is null on mains-powered devices, so it becomes nullable.
JSON
[{"deviceId":"dev-1042","ts":1795000000,"readings":{"temp":21.4,"humidity":48},"battery":87},{"deviceId":"dev-2210","ts":1795000060,"readings":{"temp":19.9,"humidity":51},"battery":null}]Zod
import { z } from "zod"; export const readingsSchema = z.object({ temp: z.number(), humidity: z.number(), }); export const telemetryItemSchema = z.object({ deviceId: z.string(), ts: z.number(), readings: readingsSchema, battery: z.number().nullable(), }); export const telemetrySchema = z.array(telemetryItemSchema); export type Telemetry = z.infer<typeof telemetrySchema>;
Understanding typing JSON
How the types are inferred
Every value in the sample is reduced to its JSON type: string, number, boolean, null, object or array. Objects found in the same array are merged, so the generator sees every key that appears in any item and counts how often it appears.
[{ "id": 1, "total": 49.5 }, { "id": 2, "total": 12, "coupon": "WELCOME" }]
→ (root type name: Orders)
export type Orders = Order[];
export interface Order {
id: number;
total: number;
coupon?: string;
}Because coupon appears in only one of the two orders, it becomes optional. The more representative your sample, the more accurate the types — include an item with every optional field if you can.
What a sample can't tell you
- A field that is always
nullin the sample is typed asnull. Replace it with the real type, such asstring | null. - An empty array becomes
unknown[], because there's nothing to infer the item type from. - Dates arrive as strings in JSON, so they're typed
string. Parse them intoDateobjects in your code if you need to. - String fields with a fixed set of values, such as a status, are typed
string. Narrow them to a union like"paid" | "refunded"by hand.
TypeScript types or a Zod schema?
TypeScript types exist only at compile time: they describe what you expect, but nothing checks that the API actually sends it. A Zod schema validates data at runtime, and z.infer gives you the matching type, so one definition covers both.
const user = userSchema.parse(await res.json()); // throws if the shape is wrong
// user is fully typed from here onUse plain types for data you trust, such as your own config. Use Zod at the edges of your app — API responses, webhooks and form input — where bad data can arrive.
interface or type?
For plain object shapes they're interchangeable. Interfaces can be extended and merged; type aliases can also describe unions and other non-object types. Pick whichever your codebase already uses.
Frequently asked questions
How are optional properties detected?
Objects in the same array are merged. A key that's present in some items but missing from others becomes optional. With a single object, nothing can be detected as optional, so paste an array of several items if you can.
How are nested types named?
After their key in PascalCase: an address key gives Address. Items of an array are named after the singular form of the key, so orders gives Order. Identical shapes share one type; if two different shapes would get the same name, a number is added, such as Address2.
Why is a field typed as null?
It was null everywhere in your sample, so there's no way to know its real type. The tool lists these fields under the output; replace them with the actual type, such as string | null.
Which Zod version does the schema target?
The output only uses z.object, z.array, z.union, z.record(z.string(), z.unknown()), .optional() and .nullable(), which work in both Zod 3 and Zod 4.
Can I generate types for a PATCH request?
Yes. Turn on All properties optional to mark every property as optional, which matches a partial update body.
Is my JSON uploaded?
No. The JSON is parsed and the types are generated by JavaScript in your browser tab. Nothing is sent anywhere.