Output will appear here
Output will appear here
Paste JSON on the left and this converter writes TypeScript interfaces on the right. It reads every element of every array, so a key that appears only in some records becomes optional (?) instead of being dropped. A value seen as null becomes T | null. A field that repeats a small set of strings becomes a literal union. Nothing is uploaded.
By Ankit Aglawe · Updated · Runs entirely in your browser
Each nested object gets its own named interface. The two line items are merged, so giftWrap, present on only one of them, comes out optional.
{
"orderId": "ord_8f2a",
"status": "shipped",
"total": 42.5,
"customer": { "id": 7, "email": "[email protected]" },
"lines": [
{ "sku": "MUG-01", "qty": 2, "price": 12.0 },
{ "sku": "TEE-L", "qty": 1, "price": 18.5, "giftWrap": true }
]
}export interface Customer {
id: number;
email: string;
}
export interface Line {
sku: string;
qty: number;
price: number;
giftWrap?: boolean;
}
export interface Root {
orderId: string;
status: string;
total: number;
customer: Customer;
lines: Line[];
}state only ever holds two values and repeats them, so it becomes "open" | "closed". assignee is sometimes a string and sometimes null, so it becomes string | null, not optional.
[
{ "id": 1, "state": "open", "assignee": null },
{ "id": 2, "state": "closed", "assignee": "lee" },
{ "id": 3, "state": "open", "assignee": "kim" },
{ "id": 4, "state": "closed", "assignee": null }
]export interface Root {
id: number;
state: "open" | "closed";
assignee: string | null;
}
export type RootArray = Root[];Every code generator on this site is run on the same awkward input: a 64-bit id, a float written as 1.0, a null, a date string, a reserved word as a key, an empty array, a nested array and an array of objects whose keys differ. This is the exact output for TypeScript, generated when the page was built.
{
"id": 9007199254740993,
"price": 19.99,
"ratio": 1.0,
"deleted_at": null,
"created_at": "2024-01-15T10:30:00Z",
"class": "premium",
"tags": [],
"matrix": [[1, 2], [3, 4.5]],
"items": [{ "sku": "A1", "qty": 1 }, { "sku": "B2", "qty": 2, "note": "gift" }]
}export interface Item {
sku: string;
qty: number;
note?: string;
}
export interface Root {
id: number;
price: number;
ratio: number;
deleted_at: null;
created_at: string;
class: string;
tags: unknown[];
matrix: number[][];
items: Item[];
}A key that is present with value null is typed T | null. A key missing from some objects is marked ?. The two are kept apart because strictNullChecks treats them differently. In the edge-case output, note is optional because one item lacks it.
If every sample of a field is null, there is no evidence of its real type, so it is typed null rather than a guessed string | null. Paste a sample where the field is filled in and the type widens.
TypeScript has one number type, so 64-bit ids, floats and integers all become number. Note that JavaScript cannot represent integers above 2^53 exactly: 9007199254740993 parses as 9007199254740992. If your API sends 64-bit ids, send them as strings.
Keys like class or default are valid property names in TypeScript and are left bare. Keys that are not identifiers (user-name, 2fa, a b) are quoted, so the interface still matches the JSON exactly.
ISO 8601 timestamps stay string. JSON has no date type and JSON.parse never produces a Date, so typing them as Date would be a lie at runtime. Use the Zod or Pydantic converter if you want dates validated.
An empty array carries no element type, so it becomes unknown[], which forces a check before use. Nested arrays become number[][], and mixed arrays become a union such as (string | number | null)[].
| JSON value | TypeScript type | Notes |
|---|---|---|
| string | string | Literal union when a small set repeats |
| number (integer or float) | number | Precision ends at 2^53 |
| true / false | boolean | |
| null | T | null | null if never seen with a value |
| key missing in some objects | key?: T | Separate from nullable |
| object | export interface Name | Named after its key, PascalCase |
| array of objects | Name[] | All elements merged |
| mixed array | (A | B)[] | |
| empty array | unknown[] |
Enter a JSON object or array in the left editor, or fetch JSON from a URL. The converter accepts any valid JSON structure including deeply nested data.
The right pane instantly displays TypeScript interfaces with properly inferred types for strings, numbers, booleans, arrays, and nested objects.
Click Copy to place the interfaces on your clipboard, or download the generated code as a .ts file ready to drop into your project.
Paste the JSON into the left pane. The interfaces appear on the right as you type. Copy them or download a .ts file. The root type is called Root, and each nested object is named after its key.
Yes. Every fixture in the project test suite is compiled with tsc --strict on each change, so output that does not compile fails the build.
Objects become exported interfaces. A top-level array also gets a type alias, for example RootArray = Root[], since an interface cannot describe an array directly.
Because the key was present with a null value. Optional (?) is only used when the key is missing from some objects. Mixing the two up is a common source of runtime errors under strictNullChecks.
Interfaces vanish at compile time. If you need runtime checks, use the JSON to Zod converter, which builds a schema from the same inference and lets you derive the type with z.infer.
No. The conversion runs in JavaScript in your browser. You can open the Network tab in DevTools and watch: no request carries your data.
Need several formats at once? Convert All shows every output from one paste. Or browse all JSON converters.