Skip to content
Sealpaste

Delete all recent work?

This removes the 0 items saved in this browser. Your settings stay.

Stop saving recent work?

New work won't be saved in this browser. The 0 items already saved can stay or go.

JSON to TypeScript

JSON

Paste JSON, or

CtrlV paste · Ctrl↵ run now

TypeScript
Ready
Ln 1, Col 1
Runs in your browser. Nothing you paste is uploaded unless you share it.

Generate TypeScript types or a Zod schema from a JSON sample or JSON Lines, types from a JSON Schema, with optional type guards

Paste an API response and get TypeScript interfaces for it. Nested objects get their own named types.

How to use it

  1. Paste a JSON sample, ideally a real response with all fields present, or several samples as JSON Lines (one per line). Or choose JSON Schema as the input and paste a schema.
  2. Set the root type name, choose interface, type alias or Zod (a schema that checks data at run time, with its type), and whether each one is exported. Turn on Type guards for an isRoot() check per type.
  3. Copy the result, or download it as types.ts, and adjust names or literal types as needed.

Example

Array items are merged

Input
{"users": [{"id": 1, "name": "Ada"}, {"id": 2, "email": null}]}
Result
export interface Root {
  users: User[];
}

export interface User {
  id: number;
  name?: string;
  email?: null;
}

Fields missing from some items become optional (?).

How types are inferred

The generator reads your sample and applies a few simple rules:

  • Every object becomes a named interface; its name comes from the key (address → Address, line_items → LineItem).
  • Objects inside one array are merged into one type. A field missing from some of them is marked optional.
  • Mixed values become unions, e.g. (number | string)[]. null in the sample becomes | null.
  • Empty arrays become unknown[], since the sample doesn’t say what they hold.
  • Keys that aren’t valid identifiers are quoted: "total-count": number.
  • A name that would hide a built-in type, such as Date or Error, gets a number: Date2.

Make the sample count

The types can only be as good as the sample. Use a response where optional fields are filled in, and include several array items when they differ, or paste several responses as JSON Lines: they are merged into one type, and a field missing from some becomes optional.

Questions

Interface or type: which should I choose?
Both work for object shapes. Interfaces can be extended and merged; type aliases can express unions. Many codebases use interfaces for API data, which is the default here.
Can it make types from a JSON Schema?
Yes: choose JSON Schema as the input. Each object schema becomes a named type, "required" decides which fields get ?, "$ref" to "$defs" or "definitions" uses that type's name (a schema may refer to itself), enum and const become literal types, anyOf and oneOf a union, allOf an intersection, prefixItems a tuple, additionalProperties an index signature, and each "description" a doc comment. A "$ref" to a URL is typed as unknown, with a note, since the page downloads nothing.
Can it write a Zod schema?
Yes: choose Zod under Declare as, with a JSON sample as the input. Each object becomes a const schema, written before the schemas that use it, with type Name = z.infer<typeof NameSchema> beside it. Whole numbers get .int(), fields missing from some samples .optional(), a value seen as null .nullable(). The calls are those Zod 3 and Zod 4 share. Parse with NameSchema.parse(data), which throws on data of another shape, or safeParse to get the errors.
What do the type guards check?
With Type guards on, each type gets a function such as isRoot(value): value is Root that checks, at run time, that the value is an object and every field has the type the sample showed: optional fields may be missing, lists are checked item by item, nested objects by their own guard. Use it on data from fetch or JSON.parse before trusting it. It checks kinds, not values: a string field accepts any string.
Are dates typed as Date?
No. JSON has no date type, so ISO date strings are typed as string, which is what JSON.parse gives you at runtime.
Is my JSON uploaded?
No. Everything runs in your browser, so your JSON stays on your device. Nothing you paste is uploaded unless you share a result, which is locked with your password first.

Related tools