Convert JSON to TypeScript Types!

Input JSON to convert it into TypeScript interfaces! TypeORM, Sequelize and Mongoose models are in the tabs above.
JSON
Result
 

Convert JSON to TypeScript Types!

This tool reads a JSON value and writes TypeScript interface declarations for it. Keys are kept exactly as they are; arrays of objects are merged into one interface, keys present in only some items get ?, and mixed values become union types.

Use it to type fetch or axios responses on the frontend, to declare return types for Next.js API routes or Express handlers, or whenever a backend only documents example responses and no type definitions.

How to use

  1. Paste a response JSON, for example copied from the DevTools Network tab.
  2. Enter the root interface name in Type name (e.g. ChannelHistory); it is kept in the URL as ?name=.
  3. Save the output to a file such as types.ts and use it via import type { ChannelHistory } from "./types".

Example

Input (JSON)
{
  "channel": "general",
  "messages": [
    { "id": "m1", "user": "kim", "text": "deploy done", "reactions": ["+1"] },
    { "id": "m2", "user": "lee", "text": null, "edited": true, "attachment": { "type": "image", "url": "https://cdn.example.com/a.png" } }
  ],
  "cursor": 42
}
Output (TypeScript)
export interface ChannelHistory {
  channel: string;
  messages: Message[];
  cursor: number;
}

export interface Message {
  id: string;
  user: string;
  text: string | null;
  reactions?: string[];
  edited?: boolean;
  attachment?: Attachment;
}

export interface Attachment {
  type: string;
  url: string;
}

Both messages are merged into a single Message interface; reactions, edited and attachment appear in only one of them, so they get ?. text mixes a string and null, giving string | null. Element interfaces take the singular form of the key (messages → Message).

Supported features

  • Every declaration is an export interface, ready to import elsewhere.
  • JSON keys are not renamed, so the types match the response object one-to-one; keys that aren't valid identifiers ("a-b", "2fa") are quoted.
  • Arrays of objects merge keys from every element; keys missing from some elements become optional (?:).
  • Mixed types produce unions (string | number); null is appended last as | null. Mixed arrays become (string | number)[].
  • Empty arrays have no element type to infer and become unknown[].
  • A top-level array also produces an alias like export type Root = RootItem[];.

Use cases

Typing fetch and axios responses

Pass the type as a generic, e.g. const res = await axios.get<ChannelHistory>(url), and the editor warns you that res.data.messages[0].text may be null.

Third-party APIs without typings

For internal or external APIs with no SDK or @types package, one sample response is enough to produce a type definition file to share with the team.

Mock data and component props

Generate interfaces from Storybook or test mock JSON and use them as component props types; when the mocks drift from the components, the compiler tells you.

Notes and limitations

  • Interfaces are erased at compile time and don't check at runtime that a response really has that shape. Pair them with a schema library like zod if you need runtime validation.
  • All numbers are number (no integer/float split), and date strings are string.
  • Fields with a fixed set of values, like "status": "active", come out as string; narrow them to literal unions ("active" | "banned") yourself.
  • Identical shapes under different keys each get their own interface, and clashing names get a numeric suffix such as Profile2.

FAQ

Can I get type aliases instead of interfaces?

A type alias is used only for a top-level array or primitive; objects are always interfaces. Rewriting export interface X { ... } as export type X = { ... }; behaves the same for this purpose.

Why are optional (?) and | null kept separate?

They mean different things: ? says the key may be absent (undefined), while | null says the key is there with a null value. With strictNullChecks on, you handle the two cases differently.

Why aren't keys converted to camelCase?

A TypeScript type describes the actual object, so renamed keys would no longer match the response. If you want camelCase in your code, define a separate type for the result of your mapping function.

I need ORM entities (TypeORM, Sequelize, Mongoose).

This page emits plain type declarations only. The TypeORM, Sequelize and Mongoose tabs at the top generate entity and model code from the same JSON.