JSON to TypeScript
Paste an example of your JSON data and get TypeScript types that describe it.
Loading JSON to TypeScript… If nothing happens, please enable JavaScript.
Writing TypeScript types for an API response by hand is slow and easy to get wrong, especially when the payload is deep or some fields only show up now and then. Paste a real JSON example into this tool and it infers the types for you as you type. Every nested object becomes its own named interface, with the name taken from the key it lives under in PascalCase, so an address key produces an Address interface and a projects array produces a Project interface for its items. Objects with exactly the same shape are declared once and reused, and names that would clash with each other or with built-in types such as Date, Response or Location get a suffix so the output compiles as it is.
Frequently asked questions
How are nested objects turned into interfaces?
Why is a field marked optional?
Can I paste JSON Lines or several samples?
What happens with empty arrays and null values?
Are date strings converted to Date?
Should I choose interface or type?
What is the JSON Schema tab for?
Is my JSON sent to a server?
About JSON to TypeScript
The more samples you give it, the better the types. Paste an array of records, several JSON documents in JSON Lines format (one object per line, as in log files and NDJSON exports), or tick the option that treats a top-level array as a list of samples. The tool merges every value it sees at the same position: a field that is missing from some objects becomes optional, a field that is sometimes null gets | null (or becomes optional, if you prefer that style), a value that is a string in one record and a number in another becomes a union, and arrays of mixed items become (A | B)[]. Arrays that were always empty are typed unknown[] and fields that were only ever null are typed null, and both are listed in the notes so you know which ones to finish by hand. Keys that are not valid identifiers, such as content-type or 2fa, are quoted automatically.
You control the style of the output: the root type name, interface or type alias, whether declarations are exported, readonly properties and arrays, and semicolons. A second tab builds a JSON Schema (draft 2020-12) from the same inference, with nested objects in $defs, required lists for the fields present in every sample, integer versus number, and a date-time or date format for strings that look like ISO 8601 timestamps. JSON has no date type, so those fields stay string in TypeScript. Both outputs can be copied or downloaded as a .ts or .schema.json file. Everything happens in your browser, so private API responses and customer data never leave your device.
TypeScript's type system is Turing complete
TypeScript types were meant to describe JavaScript values, but conditional types, recursive aliases and template literal types turned the type checker into a small programming language of its own. In 2017 a GitHub issue showed that the type system is Turing complete, and people have since written parsers, arithmetic and small interpreters that run entirely inside the compiler, producing their results as types.
The practical side of this is structural typing. TypeScript does not care what an interface is called, only what shape it has, so an object literal that has the right fields satisfies an interface it has never heard of. That is why types generated from a JSON sample fit so neatly: they describe the shape of the data, and any value with that shape is accepted.