How to use the json to typescript interface generator
- Paste a JSON sample into the first box. It is checked as you type, with the line and column of any mistake.
- Set the name of the main type and choose whether to write interfaces or type aliases.
- Choose whether keys that are missing from some objects should be marked optional, and whether to add export or readonly.
- Copy the TypeScript or download it as types.ts, then check it against your real data.
How the types are chosen
Strings, numbers and booleans map to string, number and boolean, and null maps to null. All JSON numbers become number, because JSON does not separate integers from decimals in TypeScript. Arrays become T[], and an array that holds more than one type becomes a union such as (number | string | null)[]. An empty array becomes unknown[], because the sample shows nothing about its contents.
Nested objects get their own named types, named after the key: an "address" key becomes Address. Objects inside arrays get the key name followed by Item, so "orders" gives OrdersItem. If two objects have exactly the same shape they share one type, and if names clash a number is added.
Optional keys and unions
When a list holds several objects, the tool merges them. A key that some objects lack gets a question mark, so { sku: string; qty?: number }. If a key holds null in one object and a string in another, the type is string | null. Keys that are not valid names, such as "first name" or "9lives", are written in quotes.
Types made from one sample are only as complete as the sample. Paste an array with several examples to capture variation. Related tools are the JSON schema generator, the JSON formatter and validator and the CSV to JSON converter.
Privacy and limits
The conversion runs in your browser and the text is never uploaded or executed. It accepts up to 2,000,000 characters, and extremely deep nesting is cut off so the page stays responsive. The generated code is plain text for you to review before it goes into a project.
Assumptions and limits
- Types are inferred from the sample only. Real data may have other shapes.
- Every JSON number becomes number in TypeScript.
- Objects with the same keys and types share a single named type.
- The tool writes declarations only and does not validate data at run time.
Frequently asked questions
How do I convert JSON to a TypeScript interface?
Paste the JSON into the box. The tool writes an interface for the main object and one for each nested object, using the key names.
Why is a property marked optional?
Because it was missing from at least one of the objects in the sample. Turn off the option to mark every property as required.
What does unknown[] mean?
The sample had an empty array, so the tool cannot tell what the items are. Replace unknown with the real type.
Should I use interface or type?
Both describe object shapes. Interfaces can be extended and merged, and type aliases can also name unions and other forms. Pick what your codebase uses.
Does this check my data at run time?
No. TypeScript types disappear when the code is compiled, so use a validator with a schema if you need to check real data.