JSON to TypeScript

Paste JSON and get clean TypeScript interfaces — nested objects, arrays, and union types handled automatically. Nothing uploaded.

Paste a JSON sample and get clean, compilable TypeScript interfaces — nested objects become named interfaces, arrays become typed arrays, nulls widen the property's type with null, and keys that are not valid identifiers are emitted quoted so the output always compiles.

How to use JSON to TypeScript

  1. Paste a representative JSON sample.
  2. Press Generate TypeScript.
  3. Copy the interfaces into your project and rename anything you dislike — the structure is the point.

Who it is for

TypeScript developers modelling API responses, frontend engineers writing typed fetch wrappers, and code generators bootstrapping types from real payloads instead of guessing.

How it works

  • Nested objects are emitted as their own interfaces with deduplicated names, so a payload with three nested addresses produces one Address interface, not three.
  • Arrays sample their elements: a consistent array yields Element[], a mixed one a parenthesised union.
  • A null value is emitted as unknown — a property is only truly optional when it is absent from the sample.

What it cannot do

  • Types are inferred from one sample — optional fields that merely happen to be missing in the sample may be typed as required.
  • It generates type declarations, not runtime validators; pair it with a schema checker for untrusted input.

Sample input & output

You give

{"id": 42, "email": "dev@example.com", "roles": ["admin"], "deletedAt": null}

JSON to TypeScript returns

export interface RootObject { id: number; email: string; roles: string[]; deletedAt: unknown; }

Frequently asked questions

Why are some properties optional?

Properties explicitly null in the sample are marked optional with a null union — that is how "this field can be absent" is expressed in TypeScript.

Why is a key quoted in the output?

Keys that are not valid JavaScript identifiers (dashes, spaces, leading digits) must be quoted to compile — the tool does it for you.

Can it handle arrays of mixed objects?

Yes — the root array path emits a union of the element shapes rather than pretending they match.

Why does a null property emit unknown instead of an optional nullable field?

In a single JSON sample, "deletedAt": null proves the key is present in the payload with a null runtime value, but gives zero evidence whether its non-null type is string, number, or an object, nor whether the key can be omitted. Emitting unknown forces TypeScript callers to narrow the type explicitly rather than assuming string | undefined.

How are arrays containing mixed primitive or object shapes typed?

When an array contains heterogeneous elements — such as [1, "two", null] or objects with differing optional keys across array items — the generator unions the inferred element types ((number | string | unknown)[]) and merges sibling object keys, marking properties missing from some array elements as optional (?:).

Should I use TypeScript interface declarations or type aliases for API responses?

Exported interface blocks give cleaner TypeScript compiler hover diagnostics, support declaration merging when extending third-party SDK payloads, and compile away to zero bytes of JavaScript. If you need discriminated unions or mapped utility types on top of the generated root shape, wrap or alias the generated interfaces in your application layer.

How are invalid JavaScript identifier keys like "content-type" or "2fa_enabled" handled?

JSON allows arbitrary UTF-8 string keys, whereas bare TypeScript property names must follow ECMAScript IdentifierName rules (cannot start with a digit or contain hyphens or spaces). Any key that is not a valid bare identifier is automatically quoted ("content-type": string;) so tsc --strict compiles the output without syntax errors.