JSON to TypeScript Converter

JSON to TypeScript
Paste an API response, get types you can drop straight into your project.

export
readonly
null → optional
merge array items

JSON
TypeScript
// Paste JSON on the left

Turn a real API response into types you can actually commit. Paste the JSON, get named interfaces for every nested object, with optional properties inferred from the data rather than guessed.

Writing an interface by hand from a sample response is mechanical work with one trap in it: the fields that happen to be null or absent in the sample you copied. A generator that only sees one object will type those as null or omit them, and the error surfaces later against real data.

This page infers from every object in an array rather than the first one, so a field missing from some elements becomes optional and a field that is a number in one and a string in another becomes a union instead of a guess. Nested objects become named interfaces rather than inline shapes, which is what makes the result something you can actually paste into a project.

A generated interface is a description of the sample, not a contract with the server, and the distinction decides how much to trust it. Anything the API can return but did not happen to return in the response you pasted is invisible: an error shape, a field that appears only for administrators, an array that is empty here and full of objects in production. Widening the sample — several responses concatenated into an array — is the cheapest way to catch those, and it is exactly what the multi-object inference here is for.

Numbers deserve a second look before the result is used. JSON has one numeric type and TypeScript has one too, so an integer identifier and a floating-point price are both typed as number, which is correct and hides a real problem: JavaScript numbers lose precision above 2^53, so a 64-bit identifier arriving as a JSON number is already wrong before any typing happens. APIs that know this send such identifiers as strings, and a field typed as string where you expected a number is usually the server being careful rather than a mistake.

Dates are the other place where the type is honest and unhelpful. An ISO timestamp is a string as far as JSON is concerned, so it is typed as string — and it should be, because that is what the parser hands back. Turning it into a Date is a decision for the code that consumes the response rather than something a generator should assume, and marking it in the interface with a branded type or a comment is more useful than pretending JSON.parse produces a Date object.

How to use JSON to TypeScript Converter

  1. Paste a JSON response into the left pane, or press Sample to see a worked example.
  2. Set "Root name" to whatever you want the top-level type called.
  3. Choose interface or type, and toggle export, readonly and the two inference options to taste.
  4. Copy the result, or download it straight to a .ts file.

Tips & gotchas

  • Leave "merge array items" on. It inspects every element of an array instead of only the first, so a key that is missing from some records becomes optional — which is exactly the bug that bites you in production.
  • "null → optional" turns a null value into an optional property of unknown type, because a single null tells you the key can be absent but not what it holds when present.
  • Identical object shapes are emitted once and reused, so you get one Address type rather than Address, Address2 and Address3.
  • Array property names are singularised, so "posts" produces "Post[]" rather than "Posts[]".

Frequently asked questions

Why is a property typed unknown instead of string?

Because every sample for that key was null. JSON has no type information for a null, so anything more specific would be a guess. Paste a response where the field is populated and the real type appears.

Can it generate types for other languages?

Not yet — output is TypeScript interfaces or type aliases. The generated shape maps cleanly onto most language model classes if you need to translate it by hand.

Does it validate my JSON?

Yes. A parse failure is reported inline with the message from the JSON parser, so you can fix the payload before worrying about the types.