Convert TypeScript Types to JSON!

Input TypeScript interfaces or types to generate example JSON! Handy for API mocks and test request bodies.
TypeScript
Result
 

Convert TypeScript Types to JSON!

Paste TypeScript interface, type alias, enum or class declarations and get an example JSON document that satisfies them. Literal unions, Record, utility types like Pick, Omit and Partial, and interfaces chained with extends are all followed and filled in.

Use it before an API exists to prepare MSW or json-server mock data, to create Storybook props, or to show the backend team exactly which shape you want them to send.

How to use

  1. Paste type declarations from a file like types.ts. export, import and function declarations may be mixed in; only type declarations are read.
  2. Include referenced types and enums so they get filled. Type names missing from the input become null.
  3. There are no options. The first object type that no other declaration references becomes the root, so put the one you want first; an alias that fills in a generic, such as type UserPage = Page<User>, is preferred over the generic declaration itself.
  4. Copy the result into an MSW handler's HttpResponse.json(...), a test fixture or Storybook args, then adjust the values.

Example

Input (TypeScript)
type Channel = "email" | "sms" | "push";

enum Priority { Low = "low", High = "high" }

interface Notification {
  id: string;
  channel: Channel;
  priority: Priority;
  recipient: { userId: number; locale?: string };
  payload: Record<string, string>;
  readAt: Date | null;
  meta: Pick<Audit, "createdBy">;
}

interface Audit { createdBy: string; createdAt: Date }
Output (JSON)
{
  "id": "",
  "channel": "email",
  "priority": "low",
  "recipient": {
    "userId": 0,
    "locale": ""
  },
  "payload": {
    "key": ""
  },
  "readAt": "1970-01-01T00:00:00.000Z",
  "meta": {
    "createdBy": ""
  }
}

Channel is a string literal union, so it takes its first value "email"; the string enum Priority takes its first member value "low". Record<string, string> becomes an object with a single "key", Date | null resolves to the non-null side as an ISO string, and Pick<Audit, "createdBy"> keeps only the picked key. The optional locale? is included as well.

Supported features

  • Primitives: string → "", number/bigint → 0, boolean → false, any/unknown → null, Date → 1970-01-01T00:00:00.000Z (the Date.toJSON() format).
  • Literal types use their value, unions take the first member that isn't null/undefined, and intersections (A & B) and interface ... extends merge properties.
  • An enum uses its first member's value, or 0 for a numeric enum without initializers.
  • Arrays, Array<T> and Set<T> get one sample element; tuples get a value per position; Record<K, V>, Map and index signatures become {"key": V}.
  • Supports the utility types Partial, Required, Readonly, NonNullable, Pick and Omit.
  • Methods and function-typed properties don't exist in JSON and are dropped; in classes, static fields, get/set accessors and @Exclude() fields are excluded.
  • Self-referencing arrays (children: TreeNode[]) are cut off as empty arrays.

Use cases

Mock responses for MSW or json-server

Before the backend is ready, generate mock data from the API response types. The UI can be built first, and switching to the real API later keeps the same types.

Props for Storybook and tests

Create a default object from a component's props type for story args or React Testing Library render data. Every required property is present, so there are no type errors to chase.

Agreeing on request formats with the backend

Turn a frontend request type into JSON and paste it into an API design doc or issue, so backend developers who don't use TypeScript can read the structure immediately.

Notes and limitations

  • Optional properties (?) are always included; delete keys yourself to test responses where they are absent.
  • Generic parameters are substituted where the type is used: with type UserPage = Page<User>, items: T[] is filled with User, and defaults like <T = string> apply when no argument is given. Conditional types inside generics are still not evaluated.
  • Indexed access types like T["key"], conditional types and template literal types are not evaluated.
  • A class field initialized with a single literal (name = "kim", retries = 3) uses that value; other initializers like new Date() only hint at the type.
  • Runtime schema code such as Zod or io-ts is not read; write the type inferred by z.infer out as a plain interface.
  • Map and Set are shown as an object and an array, but JSON.stringify turns both into {}, so prefer Record or arrays in response types.

FAQ

Is it right that Date comes out as a string?

Yes. JSON.stringify replaces a Date with its toJSON() result, an ISO 8601 string. The receiving side still gets a string, so either type it as string or parse it.

Which value does a union like status: "active" | "inactive" get?

The first literal in declaration order, "active". Reorder the union if you'd rather show a different value.

Does it work with NestJS DTO classes?

Class declarations and decorators are parsed, so DTOs annotated with class-validator decorators convert fine, and class-transformer @Exclude() fields are left out.

How do I go from JSON to TypeScript types?

Use the JSON → TypeScript page for the opposite direction. Separate pages also generate JSON from TypeORM, Sequelize and Mongoose models.