Skip to content
Mock and Match

How to convert JSON to TypeScript (automatic interfaces)

Turn a JSON into TypeScript interfaces in seconds. See how nested objects, lists and null values are handled, and best practices for typing API responses.

Published on

Why type your JSON

When you consume an API, the result of response.json() comes back as any: TypeScript doesn't know which fields exist, doesn't warn about typos, and doesn't autocomplete. Writing interfaces by hand for large responses is tedious and easy to get wrong. Generating the types from a real example solves the problem in seconds.

Step by step

  1. Open Transform and choose the JSON → TypeScript conversion.
  2. Paste a representative JSON into the input field — for example, a response copied from your browser DevTools' Network tab.
  3. In Root name, enter the name for the main type (here, User).
  4. Click Transform, copy the result, and paste it into your types file.

Example

With this JSON as input, the tool's real output is on the right:

Input (JSON)
{
  "id": 1,
  "name": "Ana Souza",
  "email": "ana@example.com",
  "active": true,
  "tags": ["admin", "beta"],
  "address": { "city": "São Paulo", "zip": "01310-100" },
  "orders": [
    { "id": 10, "total": 149.9 },
    { "id": 11, "total": 20, "coupon": "WELCOME" }
  ]
}
Output (TypeScript)
interface Address {
  city: string;
  zip: string;
}

interface Order {
  id: number;
  total: number;
}

interface Order1 {
  id: number;
  total: number;
  coupon: string;
}

interface User {
  id: number;
  name: string;
  email: string;
  active: boolean;
  tags: string[];
  address: Address;
  orders: (Order | Order1)[];
}
  • Each nested object becomes its own interface: address generated the Address interface, and the root uses the name you chose.
  • Lists of plain values become string[], number[], and so on.
  • In the orders list, the two items have different shapes (only the second one has coupon), so the tool generated a type for each shape and joined them with |.

Using the types in your code

TypeScript
const response = await fetch("/api/users/1");
const user: User = await response.json();

console.log(user.address.city); // your editor now autocompletes and checks the fields

Careful: annotating with : User tells the compiler what to expect, but doesn't check whether the response actually has that shape. If the API changes, the error only shows up at runtime. For data coming from outside your application, pair the types with a runtime validation.

Special cases

  • Null values: a null field in the example becomes the null type. If other responses carry a string in it instead, adjust it to string | null.
  • Empty lists: with no items to inspect, the type comes out as unknown[]. Swap in the correct type, or use an example with at least one item.
  • Numbers: integers and decimals both become number.
  • Dates: JSON has no date type; they arrive as text and become string.

Best practices

A single example doesn't show everything

The types only reflect the JSON you pasted. Fields that only sometimes appear are left out, or treated as required. If you have more than one example response, generate from the most complete one and mark optional fields with ? afterward.
  • Use a real, complete response, not a hand-edited snippet.
  • Review the generated names and which fields can be null or optional.
  • If your API publishes an OpenAPI spec, generating types from it is more reliable than inferring them from an example.

Frequently asked questions

Does the tool generate type or interface?

It generates interfaces, one for each object found in the JSON. If you prefer type, just swap the word interface for type and add an equals sign before the braces.

Does it work with a large JSON?

Yes. The input field accepts up to 10 million characters, and the conversion runs in your browser, without sending the content to any server.

Can I paste my actual API response?

Conversion happens locally and nothing is sent to any server. Still, as good practice, swap out tokens, passwords and personal data for fake values before pasting any content into any website.

How do I validate the data at runtime?

TypeScript types don't exist after compilation. To validate data coming from outside your app, convert the same JSON into a Zod schema, as the JSON to Zod guide shows.

Try it now

Free, no sign-up, and everything happens in your browser.

Open Transform