Free · Instant · Runs locally

JSON to TypeScript

Paste any JSON — an API response, a config file — and get clean TypeScript interfaces with nested types, arrays, optional properties and unions detected automatically.

🔒 Conversion runs locally — your JSON never leaves the browser.

How to generate TypeScript types from JSON

📥

1. Paste JSON

Drop in an API response or any JSON document. It is parsed and validated instantly.

🧠

2. Types are inferred

Nested objects become their own interfaces; arrays are scanned so properties missing from some items become optional.

📋

3. Copy or download

Grab the generated interfaces (or type aliases) and paste them straight into your project.

Hand-writing types for a big API response is tedious and error-prone — this generator scans every object in every array, merges the shapes, marks missing keys optional, and unions mixed primitive types (string | number). Sub-objects get their own named interfaces so the output reads like hand-written code. Tidy the input first with the JSON Formatter, or compare two payloads with JSON Diff.

What can and cannot be inferred from a sample

Generating types from a JSON sample is inference from a single observation, and it produces a description of that sample rather than of the underlying schema. Everything present becomes required; everything absent does not exist. If the sample happens to omit an optional field, the generated type will not include it, and code written against that type will fail to compile when the field appears.

The reverse error is equally common: a field that is null in the sample but sometimes carries a value is inferred as null, when it should be a nullable union. A field that is an empty array gives no information about its element type at all, and the honest inference is unknown[] rather than a guess.

The practical mitigation is to generate from several representative samples rather than one — ideally including responses with optional fields present and absent — and then to read the result critically. Treat generated types as a first draft that captures the shape, not as a specification.

Optionality, null and undefined

TypeScript distinguishes three things JSON blurs. A property marked ? may be absent from the object. A property typed | null is always present but may hold null. A property typed | undefined is present with an undefined value, which JSON cannot represent at all since there is no undefined literal.

Getting this wrong produces bugs that type checking was supposed to prevent. Marking a field optional when the API always sends it but sometimes sends null means obj.field passes the check and is null at runtime. Under strictNullChecks the distinction is enforced, and without that flag much of TypeScript's value is lost — it should be on.

Arrays of heterogeneous objects are the other structural decision. Inference typically produces a union of the observed shapes, which is accurate but awkward to consume. Where the objects share a discriminating field, converting the union into a discriminated union — a literal type on a common property — lets TypeScript narrow correctly in a switch, which is far more useful than a bare union.

Types are erased, so validate at runtime

The most important thing to understand about generated types is that they provide no runtime guarantee whatsoever. TypeScript types are erased at compile time; nothing checks that the JSON arriving from a network call matches the interface you declared. An API that changes its response shape produces no error at the boundary — instead you get an undefined value several layers deeper, at a point where the cause is no longer obvious.

This is why casting a parsed response with as MyType is a lie to the compiler. It suppresses checking without establishing anything. The response is unknown until something actually inspects it.

The robust pattern is a runtime validation library — Zod, Valibot, io-ts and similar — where you declare a schema once, validate the response against it at the boundary, and derive the static type from that schema. You get one source of truth, a genuine error at the point of failure with a message naming the offending field, and types that cannot drift from the validation. Generated interfaces are a reasonable starting point for writing that schema.

JSON to TypeScript FAQ

How are optional properties detected?

When converting an array of objects, the generator merges all item shapes. A key present in some items but not others becomes optional (name?: string), matching how you would type it by hand.

What happens with null values?

A field that is null in the sample becomes a union with null (string | null) when other samples show a type, or unknown when null is all the generator has seen.

Interfaces or type aliases — which should I use?

Functionally equivalent for object shapes. Interfaces merge declarations and read conventionally for API models; type aliases compose better with unions. The toggle outputs either style.

Is my JSON sent anywhere?

No — parsing and generation run entirely in your browser, so API responses with real data stay private.

Why are all my fields marked required?

Inference from one sample marks everything present as required. Generate from several samples covering optional fields, and edit the result — a single response cannot reveal which fields are optional.

Do these types validate the data at runtime?

No. TypeScript types are erased at compile time and check nothing at runtime. Use a validation library such as Zod at the network boundary and derive the type from the schema.