JSON to TypeScript Converter – Generate Interfaces, Type Aliases and Zod Schemas | TrenTyx

trentyx@local:~/data-converters/json-to-typescript $ run json-to-typescript_

Paste an API response and get types you can actually commit. Arrays of objects are merged rather than sampled, so a field missing from one element becomes optional instead of silently disappearing, and null becomes part of the union instead of collapsing to any. Output as TypeScript interfaces, type aliases or a Zod schema.

Paste JSON on the left. Types regenerate as you type.
0
interfaces
0
properties
0
optional fields
0
nesting depth
input.json empty
types.ts

How the Types Are Inferred

Most converters read the first element of an array and assume the rest look the same. Real API responses do not behave that way: one record has a deletedAt, the next does not; one avatar is a string, another is null. This generator walks every element and merges what it finds.

  • Missing keys become optional — a field present in 9 of 10 objects is emitted as field?: string.
  • Mixed types become unionsstring | number rather than the first type seen.
  • Nulls are keptstring | null, so strict null checks still work.
  • Empty arrays stay honestunknown[], because nothing in the sample proves what belongs there.
  • Identical shapes are merged — repeated address objects produce one interface, not five copies.
  • Nested types get real names — derived from the key and de-pluralised, so orders yields an Order interface.

Interfaces, Type Aliases or Zod

Interfaces are the usual choice for object shapes: they can be extended and merged, and error messages read better. Type aliases are the right call when you want to compose with unions and mapped types later. Zod schemas go one step further and validate at runtime — useful precisely because a TypeScript type is erased at compile time and proves nothing about what the server actually sent. With Zod you get both: the schema checks the payload, and z.infer gives you the static type for free.

Common Use Cases

  • Typing a third-party API that ships no types — paste a real response, get a starting point.
  • Writing fixtures and mocks — generate the shape once, then keep tests honest against it.
  • Migrating JavaScript to TypeScript — turn existing payloads into declarations instead of guessing.
  • Validating webhooks — emit a Zod schema and reject malformed deliveries at the edge.
  • Reviewing an unfamiliar payload — the generated types are often a faster way to read a deeply nested response than the JSON itself.

Frequently Asked Questions

Is my JSON sent anywhere?

No. Parsing and generation happen in your browser with JSON.parse and plain JavaScript. Nothing is uploaded, which matters when the payload you are typing contains production data.

Why did a field become optional when it exists in my data?

Because it was absent from at least one element of an array. That is inferred from the sample you pasted, not from a schema — if the field is genuinely always present, include a more representative sample or remove the question mark by hand.

Can it detect dates, enums or IDs?

Dates, optionally: enable ISO strings as Date and values matching an ISO 8601 timestamp are typed as Date (in Zod, z.coerce.date()). Enums are deliberately not inferred — a sample of three status values cannot tell you whether a fourth exists, and a wrong union is worse than a plain string.

What happens to keys that are not valid identifiers?

They are quoted, so "content-type" and "2fa" stay valid TypeScript. Access them with bracket notation.

How large a payload can it handle?

Multi-megabyte responses work, though generation is debounced and very wide arrays take a moment. If you only need the shape, a few representative elements produce the same types as ten thousand.

Related Tools