Skip to content

JSON to TypeScript

Turn a JSON sample into TypeScript interfaces or type aliases. Nested objects become their own named types, arrays of objects are merged so keys that are missing in some items become optional, and mixed values become union types. Paste an API response, choose interface or type and copy declarations you can drop straight into your code.

Reset

Frequently asked questions

How are nested objects handled?

Every nested object gets its own declaration, named after the key that holds it in PascalCase, for example address becomes Address. Objects inside arrays are named after the singular of the array key, so users becomes User and categories becomes Category. Objects with exactly the same fields share one declaration.

When does a property become optional?

When an array holds several objects and a key is missing from some of them, that key is marked optional with a question mark. Keys that appear in every item stay required. A single object has no other samples to compare, so all of its keys are required.

How are null values typed?

A key that is null in your sample is typed as null, and a key that is null in some items and a string in others becomes string | null. If a field can be null in real data but is never null in your sample, add the null by hand or paste a sample that includes it.

Should I choose interface or type?

Both describe the same shape. Interfaces can be extended and merged, which many style guides prefer for object shapes; type aliases also work for unions and are what some teams use everywhere. Choose whichever your code base uses, the fields are identical.

How are arrays typed?

An array of one kind of value becomes that type with [] after it, such as string[] or User[]. An array with several kinds becomes a union in parentheses, such as (string | number)[]. An empty array becomes unknown[], because the sample says nothing about its items.

What happens to keys that are not valid identifiers?

Keys with spaces, dashes or other characters that are not allowed in a TypeScript identifier are written in quotes, for example "first-name": string, which is valid TypeScript and matches the JSON exactly.

Are numbers typed as integer or float?

TypeScript has one number type, so integers and decimals both become number. If you need to know which values are whole numbers, the JSON Schema Generator distinguishes integer from number.

Can I rename the root type?

Yes. Enter a name for the root type, such as ApiResponse. It is converted to PascalCase, and the nested types keep the names derived from their keys. When the top level of your JSON is an array or a single value, the root becomes a type alias for it.

Are dates detected?

No. JSON has no date type, so a date in your sample is a string and is typed as string. Convert it to a Date in your code after parsing, or narrow the type by hand if you use a template literal type.

How large can the JSON be?

Up to 1,000,000 characters, nested up to 64 levels deep. The sample is only analysed to produce the types and is not stored.

About OneClick Pro

OneClick Pro is a free collection of more than 150 web tools for everyday work: converting images and units, formatting and validating code, checking domains and SEO, and working with text, dates and numbers. Every tool runs without registration or subscription, and what you enter is only used to calculate your result and is never stored. Use it as a quick toolbox for one-off tasks, or bookmark the tools you rely on every day.