Skip to content

JSON to TypeScript and Zod

Turn a JSON sample into TypeScript interfaces or a Zod schema, with optional and nullable fields worked out.

JSON sample

0 characters

TypeScript types

Paste a JSON sample to get types for it. The more varied the sample, the more accurate the types.

Runs in your browser — nothing you enter leaves this device.

About this tool

What it does

This tool reads a JSON sample — an API response, a config file, a webhook payload — and writes the TypeScript types or the Zod schema that describe it. It looks at every item in every list, not just the first, so a field that is missing from some items comes out optional, a field that is sometimes null comes out nullable, and a list that mixes numbers and strings gets a union. Nested objects become their own named types, named after the key that holds them, and two objects with the same shape share one type instead of being written out twice.

How to use it
  1. Paste a JSON sample on the left, or press “Try an example”.
  2. Choose TypeScript for interfaces, or Zod for a runtime schema with its inferred types.
  3. Set the root type name — the name of the outermost type, such as User or OrderResponse.
  4. For TypeScript, turn on “Use type aliases” if your codebase prefers type over interface.
  5. Copy the code into your project and adjust anything your sample could not show.
Limits and your data
  • Types come from the sample, so they are only as complete as it is. A field that is null in every item is typed as null; a list that is empty is typed as unknown[]. Paste a response that shows the real variety.
  • Every string is typed as string. The tool does not guess dates, email addresses or enums from a few values, because a guess that is wrong is worse than a plain string you can narrow yourself.
  • Numbers are typed as number. JSON does not distinguish integers, so the Zod output uses z.number() rather than z.number().int().
  • Type names come from keys: orders becomes Order, categories becomes Category. When two different shapes share a name, the second gets a number, such as Meta2.
  • The sample can be up to 2 MB.
  • The sample is parsed and the code is written in your browser. Nothing is uploaded, so you can paste real responses, but it is still good practice to remove tokens and personal data from anything you commit.

Questions

Why is a field marked optional when my API always sends it?

Because at least one item in the sample did not have it. If the API really always sends it, the sample was incomplete; remove the question mark, or paste a sample where every item has the field.

What is the difference between the TypeScript and Zod output?

TypeScript types exist only while you write code and are removed when it is compiled. A Zod schema also runs: it checks data at runtime, for example a response from a server you do not control, and its inferred type gives you the same TypeScript type from one definition.

Does it handle keys with dashes or spaces?

Yes. Keys that are not valid identifiers are written in quotes, such as "content-type": string, which TypeScript and Zod both accept.

Can I generate a JSON Schema instead?

Not here. For checking data against a JSON Schema you already have, use the JSON Schema Validator.