JSON to TypeScript Converter
Paste an API response and get ready-to-use TypeScript interfaces or type aliases, with nested types, optional fields and null unions inferred for you.
Runs in your browserNo sign-upNothing uploaded
How to use the JSON to TypeScript
- Paste a JSON sample, such as an API response, into the left box or open a
.jsonfile. - Set the root type name (for example
UserorApiResponse). - Choose
interfaceortypedeclarations and toggleexport,readonlyor all-optional fields. - Pick
unknown(safer) oranyfor values that the sample cannot reveal, such as empty arrays and nulls. - Copy the generated code or download it as a
.tsfile.
How the types are inferred
- Primitives: strings, numbers and booleans map to
string,numberandboolean. - Nested objects become their own named interfaces in PascalCase, so
billing_addressbecomesBillingAddress. - Arrays of objects are merged into one item type, named in the singular:
usersgivesUser[]. A key that is missing from some items becomes optional (key?:). - Null values combine with the other values seen for that key:
string | null. A key that is only evernullfalls back tounknownorany. - Mixed arrays become union arrays such as
(number | string)[]. - Identical shapes are declared once and reused, and different shapes that would share a name get a numeric suffix.
- Keys that are not valid identifiers, such as
"first-name"or"2fa", are quoted.
interface vs. type
For plain object shapes the two are nearly interchangeable. interface declarations can be extended and merged, and give slightly clearer compiler errors. type aliases can also express unions and mapped types. Many teams use interfaces for API models and types for everything else; choose whichever your codebase or linter (@typescript-eslint/consistent-type-definitions) expects.
Get better types from your sample
Types can only be as complete as the sample. Paste a response that includes optional fields, or an array with several varied items, so the generator can detect optional and nullable keys. Values such as dates and enums arrive as plain strings in JSON; narrow them by hand afterwards, for example status: 'active' | 'disabled'. For runtime validation, pair the generated types with a schema library such as Zod.
Frequently asked questions
Is my JSON uploaded?
How are optional properties detected?
?. With a single object every key is required, unless you tick All optional.Should I use <code>unknown</code> or <code>any</code>?
unknown is safer: TypeScript makes you narrow the value before using it. any turns type checking off for that value. Use it only if you plan to refine the types later.Why is an ISO date typed as <code>string</code>?
new Date() after parsing, change the property to Date in your model.