JSON to Types

Paste a JSON example and generate matching TypeScript interfaces, Python or Go types. Everything is computed in your browser.

100% client-side

JSON input

0 characters

Generated types


        

What is JSON to Types?

JSON to Types turns a JSON example — from an API response, a config file, or a database export — into the equivalent type definitions for TypeScript, Python, or Go, so you don't have to hand-write and maintain interfaces that drift out of sync with the real data. It infers each field's type from the actual values, detects which fields are optional or nullable, and generates one named type per distinct object shape.

How to use JSON to Types

  1. Paste a JSON example into the JSON input panel, or click Example.
  2. Set a Root type name if you want something other than "Root".
  3. Pick TypeScript, Python, or Go as the output language above the Generated types panel.
  4. Read the generated types and click Copy to grab them.

How JSON to Types works

The tool walks your JSON value by value: every object becomes a named type (interface, TypedDict/dataclass, or struct), every array's elements are inspected and merged into one element type, and every primitive maps to its closest TypeScript/Python/Go equivalent. When an array holds several objects, it compares them field by field — a key present in every object is required; a key missing from at least one object becomes optional; a key whose value is sometimes null becomes a nullable type instead (these are two different, independently-detected things). When array elements have genuinely different primitive types (e.g. a mix of numbers and strings), the tool keeps a real union type rather than silently widening everything to any.

// Input
{
  "id": 1,
  "name": "Ada",
  "tags": ["pioneer", "mathematician"],
  "roles": [
    { "name": "admin", "level": 1 },
    { "name": "editor", "level": 2, "temporary": true }
  ]
}

// Generated TypeScript
interface Role {
  name: string;
  level: number;
  temporary?: boolean;
}

interface Root {
  id: number;
  name: string;
  tags: string[];
  roles: Role[];
}

Two nested objects that turn out to have the exact same shape (say, a "billingAddress" and a "shippingAddress" with identical fields) are merged into one generated type instead of being duplicated, and a JSON key that isn't a valid identifier in the target language (like "foo-bar") is handled per language: quoted in TypeScript, rendered as a functional TypedDict(...) call in Python, or kept as the real key in a Go json tag next to a sanitized Go field name. Go has no native union type, so a field whose values genuinely vary in shape falls back to Go's any.

Frequently asked questions

How does it decide when a field is optional?

When your JSON input is an array of objects, a field is marked optional only if it is missing from at least one object in that array. A field that's always present but sometimes null becomes a nullable type instead (e.g. "string | null"), which is a different thing from being optional.

What happens with an empty array?

There's nothing in an empty array to infer an element type from, so it's typed as unknown[] (TypeScript), List[Any] (Python) or []any (Go) and the tool shows a note explaining why.

Does it detect nested object shapes?

Yes. Every nested object gets its own named interface/class/struct, named after its parent key. If two different keys produce the exact same shape, they share one generated type instead of duplicating it.

Is the Go or Python output guaranteed to compile as-is?

It's meant to be a strong starting point, not a guarantee. JSON keys that aren't valid identifiers are handled (quoted in TypeScript, a functional TypedDict in Python, a sanitized name plus the real key in a Go json tag), but you should still skim the generated code before using it.