BrowserTools
Home / Converters / JSON to TypeScript

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?
Each object that has at least one key becomes a separate named interface. The name comes from the key that holds it, converted to PascalCase: billing_address becomes BillingAddress. Items of an array get the singular form of the key, so orders gives Order and categories gives Category. The root object uses the name you type in the Root name box.
Why is a field marked optional?
Because it was missing from at least one of the objects that were merged at that position. If you paste a single object, every field is required. If you paste an array of objects, or JSON Lines, a field only stays required when every sample contains it. With the "Optional instead of | null" option, fields that were sometimes null are also written as optional instead of getting | null.
Can I paste JSON Lines or several samples?
Yes. If the text is not one JSON document but every non-empty line is valid JSON, it is read as JSON Lines and all lines are merged into one type. For a regular JSON array you can tick "Top-level array is a list of samples" so the items are merged into the root type itself instead of producing type Root = RootItem[].
What happens with empty arrays and null values?
An array that was always empty gives no information about its items, so it is typed unknown[]. A field that was only ever null is typed null. Both cases are listed in the notes below the output. The best fix is to paste a sample where those fields have real values, since the tool can only describe the data it sees.
Are date strings converted to Date?
No. JSON has no date type, so after JSON.parse a timestamp is still a string, and typing it as Date would be wrong at runtime. The tool keeps it as string, mentions in the notes which fields look like ISO 8601 dates, and adds format date-time or date to those fields in the JSON Schema.
Should I choose interface or type?
For plain object shapes they are almost interchangeable. Interfaces can be extended and merged, and many style guides prefer them for data models. Type aliases are needed for unions and are preferred by some teams for consistency. When the root is not an object, for example an array or a primitive, the root is always a type alias, because an interface cannot describe those.
What is the JSON Schema tab for?
JSON Schema describes the same structure in a language-neutral format that validators such as Ajv, OpenAPI tooling and many editors understand. The generated schema uses draft 2020-12, puts nested objects in $defs and links them with $ref, lists fields present in every sample under required, and distinguishes integer from number. Use it as a starting point and tighten it with patterns, enums or limits.
Is my JSON sent to a server?
No. Parsing and type inference are plain JavaScript running in your browser. Nothing you paste or open is uploaded or stored, so you can use real API responses, tokens or personal data safely.

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.

Support