JSON to TypeScript and Python Types

Turn a sample document into type definitions you can paste into your project - TypeScript interfaces, Python dataclasses, Pydantic models, or a JSON Schema.

What gets generated

Types are inferred from the values in the document: strings, numbers, booleans, nulls, arrays and nested objects each map to the natural type in the target language, and nested objects become their own named types.

A document and its TypeScript
{"id": 7, "name": "Ada", "tags": ["py"], "meta": {"active": true}}

interface Meta {
  active: boolean;
}

interface Root {
  id: number;
  name: string;
  tags: string[];
  meta: Meta;
}

Choosing an output format

Four formats are available, and they answer different questions.

  • TypeScript interfaces - for typing an API response at the boundary of a front end.
  • Python dataclasses - plain, dependency-free structures for scripts and internal code.
  • Pydantic models - when you want the data validated at runtime, not just described.
  • JSON Schema - for validating documents in a pipeline, or for sharing the shape with people who are not writing your language.

Working from one sample

Types generated from a single document describe that document, not the API behind it. A field that happens to be null in your sample cannot be typed from it, and a field that is missing in your sample will not appear at all. Treat the output as a first draft to correct against the API documentation, which is still much faster than writing it out by hand.

Two cases deserve a second look before you commit the output. A null in the sample is typed as null, when the field is almost always "string or null" - mark it optional or make it a union. And an array is typed from its first element, so a list whose entries are not all the same shape will be described by whichever one happened to come first.

Python dicts work as input too

The generator reads whatever is in the input box, and that box accepts Python literal syntax as readily as JSON. So you can paste the repr of a response you captured in a Python shell and get TypeScript interfaces for it, without converting anything first.

From a Python dict to a dataclass
{'id': 7, 'name': 'Ada', 'active': True}

@dataclass
class Root:
    id: int
    name: str
    active: bool

Questions

How do I convert JSON to a TypeScript interface?
Load the document, open the Tools panel and choose Code. Select TypeScript and copy the generated interfaces.
Can it generate Python dataclasses or Pydantic models?
Yes - both, along with TypeScript interfaces and JSON Schema. A Python dict works as input just as well as JSON.

Open JSON Explorer and try it on your own data