JSON to TypeScript
Turn an API response into TypeScript interfaces, with optional fields worked out from the sample.
About the JSON to TypeScript
Paste an array rather than a single object whenever you can. Every element is compared against the others, so a key present in some records and missing from others comes out optional rather than required — which is the difference between a type that holds up against real data and one that quietly lies to you.
This is inference from an example, and an example cannot tell you everything. A field that happens to be `null` in your sample might be a string in every other record; an empty array says nothing at all about what it holds. Both cases are called out above the output rather than guessed at silently, because a wrong type you were not warned about is worse than an honest `unknown`.
Nested objects each get their own interface, named from the key that holds them, and plural keys are singularised — so `users` becomes `User[]` rather than `Users[]`. Keys that are not valid TypeScript identifiers are quoted.
The whole thing runs in your browser, which matters here more than it looks: the JSON people paste into a type generator is usually a real response from a real API, complete with real customer data.
How it works
Paste a JSON sample — a whole array of records gives a better result than one.
Choose interfaces or a single nested type, and whether fields should be readonly.
Copy the generated types into your project.
Frequently asked questions
- Should I paste one object or an array?
- An array, if you have one. The generator compares elements against each other, so fields that appear in some records but not others come out optional. From a single object every field looks required, which is rarely true of a real API.
- How does it decide a field is optional?
- By seeing it missing from at least one record in the sample. That only works when there is more than one record to compare, which is the argument for pasting the full array.
- What happens to null values?
- A field that is null in some records and a value in others becomes a union with `null`. A field that is null in every record cannot be resolved from the sample at all, so it is typed `null` and flagged for you to widen by hand.
- Why is my array typed unknown[]?
- Because it was empty in the sample. There is nothing in it to infer an element type from, so `unknown` is the honest answer — guessing would give you a type that compiles and does not describe your data.
- Should I use interfaces or a type alias?
- Interfaces for anything you will reuse or extend, which is most API models. A single nested type alias is handier for a one-off shape you want to keep in one place.
Privacy
Everything happens locally. Your files are read by your own browser, processed on your device, and never uploaded — closing the tab is all it takes to erase them.
Related tools
CSV to JSON Converter
Convert CSV to JSON and back, with proper quoting, type inference and a table preview.
JSON Formatter
Format, validate and minify JSON with clear error positions.
JWT Decoder
Decode a JSON Web Token's header and payload, and verify an HMAC signature.
JavaScript Formatter
Beautify JavaScript or TypeScript with Prettier, including JSX.
JSON Tree Viewer
Explore large JSON as a collapsible tree, with search and copyable key paths.
MD5 Hash Generator
Generate an MD5 checksum for text or files, and verify a download against a known digest.