JSON to Types

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

Updated:

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.

Examples

1. A Go struct for an API response that contains a null. With Root type name set to User, snake_case keys become exported Go fields with json tags, so encoding/json still maps them to the original names. A null carries no type information, so last_login comes out as any; replace it with the real type (a pointer such as *string if it can be null). The tool writes UserId; rename it to UserID if you follow Go's initialism style, since the tag keeps the mapping:

// JSON
{"user_id": 42, "email": "[email protected]", "last_login": null}

// Go
package main

type User struct {
	UserId int `json:"user_id"`
	Email string `json:"email"`
	LastLogin any `json:"last_login"`
}

2. An array whose items don't all have the same fields. With the root name Product, both items are merged into one type: discount only appears in the second, so it becomes optional, and the root is an array of that type. The Python dataclass tab gives the same shape as price: float and discount: Optional[float] = None:

// JSON
[{"sku": "A1", "price": 9.5}, {"sku": "B2", "price": 12, "discount": 0.1}]

// TypeScript
interface Product {
  sku: string;
  price: number;
  discount?: number;
}

type Root = Product[];

If your sample data lives in a spreadsheet, turn it into JSON first with the CSV to JSON Converter; if the JSON won't parse, the JSON Formatter shows where the error is.

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.