Turn a JSON example into TypeScript interfaces — nested objects become named interfaces, arrays of objects merge with optional keys, and mixed types become unions.
Show calculation steps
Processed privately in your browser — nothing you paste is uploaded, logged or stored.
From payload to types
Hand-writing interfaces for a big API response is tedious and error-prone. Paste the JSON and this converter emits TypeScript: every nested object becomes a named interface (PascalCase, singularized from array keys — a lines array yields interface Line), arrays of objects are merged so keys missing from some elements come out optional, and fields holding mixed types become union types like string | null.
Worked example
Input:
{
"id": 7,
"customer": { "name": "Meera", "email": "[email protected]" },
"lines": [
{ "sku": "A-1", "qty": 2, "note": "gift wrap" },
{ "sku": "B-2", "qty": 1 }
],
"discount": null
}Output:
export interface RootObject {
id: number;
customer: Customer;
lines: Line[];
discount: null;
}
export interface Customer {
name: string;
email: string;
}
export interface Line {
sku: string;
qty: number;
note?: string;
}note? is inferred from evidence — the second line item lacks it. discount: null is what one sample can prove; widen it to number | null if the API sends values there.
When to use it
- Typing third-party API responses you do not control.
- Migrating a JavaScript codebase to TypeScript one payload at a time.
- Generating a first draft of DTO types from captured production data.
Limitations to know
- Types describe your sample, not the API’s full contract — rare optional fields absent from the sample will be missing. Feed the most complete example you have.
- A single object cannot reveal optionality; only arrays with varying elements can. Review
?markers against the real API docs. - Empty arrays become
unknown[]on purpose — honest and type-safe, unlikeany[].
Common errors and fixes
- Interface named
Prop123or quoted keys like"content-type"— the JSON keys are not valid identifiers; the converter quotes them correctly, no action needed. - Two interfaces named
UserandUser2— two different shapes shared the key name; rename one or align the shapes upstream. - Union noise like
(string | number)[]— the source array genuinely mixes types; clean the data or keep the union, it is accurate.
If you also need runtime validation to match, generate the contract with the JSON to JSON Schema converter from the same sample.
How to use the JSON to TypeScript Converter
- Paste a JSON example — an API response or config object.
- Set the root type name, and choose interface or type-alias output (readonly optional).
- Click "Generate TypeScript".
- Copy the interfaces into your .ts file and adjust optionality where your API allows it.
Frequently asked questions
How are nested interfaces named?
From the property key, converted to PascalCase, with array keys singularized — a "lines" array of objects becomes interface Line. Name collisions get a numeric suffix instead of silently merging different shapes.
How does it decide which properties are optional?
From evidence in arrays: when elements of the same array have different keys, the missing ones become optional (note?: string). A single object cannot reveal optionality, so its keys are emitted required — adjust manually where your API says otherwise.
What happens with mixed value types?
They become union types. A field holding a string in one place and null in another is typed string | null, and mismatched array elements produce unions of their member types.
Why does an empty array become unknown[]?
The sample gives no evidence of the element type. unknown is TypeScript's honest answer — it forces you to check the type before use, unlike any, which would switch the checker off.