JSON to TypeScript Interface Generator

Paste an API response or config file and get matching TypeScript interfaces — or a Zod schema — with optional and nullable fields detected from every array item.

TypeScript3 declarations
export interface User {
  id: number;
  full_name: string;
  tags: string[];
  "billing-address": BillingAddress;
  shipping: BillingAddress;
  orders: Order[];
}

export interface BillingAddress {
  city: string;
  zip: string;
}

export interface Order {
  id: string;
  total: number;
  coupon: string | null;
  notes?: string;
}

What this generator does

When you call an API from TypeScript, response.json() gives you any, and every typo in a property name slips through the compiler. Writing interfaces by hand for a large payload is tedious and easy to get wrong. This tool reads a real JSON sample and writes the declarations for you: one interface per object structure, arrays typed from all of their elements, and nullable or missing fields marked so the compiler forces you to handle them.

How to use it

  1. Paste your JSON into the JSON panel, or click Load example.
  2. Set the Root type name (for example User or OrderResponse).
  3. Pick the Output (TypeScript or Zod schema), the Declaration style (interface or type alias) and whether to Add export.
  4. The code updates as you type. Click Copy or Download .ts.

Worked example

The built-in example contains a hyphenated key, two objects with the same structure, and an array of orders where one order has an extra field and one coupon is null:

{
  "id": 1,
  "full_name": "Alice",
  "tags": ["admin"],
  "billing-address": { "city": "Madrid", "zip": "28001" },
  "shipping": { "city": "Paris", "zip": "75001" },
  "orders": [
    { "id": "A1", "total": 19.9, "coupon": null },
    { "id": "A2", "total": 5, "coupon": "SAVE5", "notes": "gift" }
  ]
}

With the root name User, the tool generates:

export interface User {
  id: number;
  full_name: string;
  tags: string[];
  "billing-address": BillingAddress;
  shipping: BillingAddress;
  orders: Order[];
}

export interface BillingAddress {
  city: string;
  zip: string;
}

export interface Order {
  id: string;
  total: number;
  coupon: string | null;
  notes?: string;
}

full_name keeps its underscore, the hyphenated key is quoted, orders becomes Order[] (the array name is singularised), coupon is nullable because one order has null, and notes is optional because only one order has it. With Output set to Zod schema, the same order becomes z.object({ id: z.string(), total: z.number(), coupon: z.string().nullable(), notes: z.string().optional() }).

Options and type rules

  • interface vs type alias: interfaces can be extended and merged; type aliases are needed for unions. Non-object roots (an array, a string) are always emitted as a type alias.
  • Add export prefixes every declaration with export so you can drop the file into a module.
  • Names are PascalCase versions of the keys. If a name is already taken by a different structure, a number is appended (Root2); names that clash with built-ins such as Date get a Type suffix.
  • Numbers are always number; JSON does not distinguish integers. Date strings stay string, because JSON has no date type.
  • Zod output targets the standard z API (works with Zod 3 and 4) and also exports a z.infer type for each schema, giving you runtime validation and compile-time types from one source.

Limitations

  • Types describe only what the sample contains. A field that can be null in production but is never null in your sample will not get | null.
  • Literal and enum types are not inferred: "status": "active" becomes string, not "active" | "inactive".
  • Arrays mixing objects of very different shapes are merged into one interface with many optional properties rather than a discriminated union.
  • Recursive structures (a tree of comments) produce one declaration per nesting level instead of a self-referencing type.

Tips

  • Paste an array of several real responses rather than one, so optional and nullable fields are detected correctly.
  • Need a language-neutral contract for validation in another service? Generate one with the JSON Schema Generator.
  • If the JSON will not parse, find the exact error position with the JSON Formatter, or explore a large payload first in the JSON Viewer.

Frequently Asked Questions

Does it rename my JSON keys to camelCase?

No. Property names are kept exactly as they appear in the JSON, so the types match the data you actually receive. Keys that are not valid identifiers, such as "billing-address", are written in quotes.

How are arrays of objects handled?

Every element is inspected and merged into one interface. A key that is missing from some elements becomes optional (notes?: string), and a field that is null in some elements gets | null.

What happens with mixed or empty arrays?

Mixed arrays produce a union, for example (string | number | null)[]. An empty array produces unknown[] because there is nothing to infer from, and an empty object produces Record<string, unknown>.

Can I generate a Zod schema instead?

Yes. Choose "Zod schema" under Output to get z.object() schemas with .optional() and .nullable() where needed, plus a z.infer type for each one. Nested schemas are declared before the schemas that use them.

Why do two keys share the same interface?

When two objects have exactly the same structure, the generator declares the interface once and reuses it, so billing-address and shipping can both be BillingAddress. Rename it in your editor if you prefer a more generic name such as Address.

Is my JSON sent to a server?

No. The conversion runs entirely in your browser; your JSON never leaves your device.