Youtilities

Popular

All tools

JSON to TypeScript

Turn any JSON into TypeScript interfaces. Nested types named, optional keys detected.

Runs in your browser. Nothing is uploaded.

Input

Waiting for JSON

TypeScript
TypeScript types appear here.

What this does

Paste an API response or any JSON and get TypeScript interfaces for it. Nested objects become their own named types; arrays of objects are merged into one type.

Optional fields

If a key is missing from some items in an array, it is marked optional with ?. Turn on All optional when the API can leave out anything. A value that is sometimes null gets | null.

Names

Types are named after their keys: users becomes User, address becomes Address. Two objects with the same shape share one type. Change the root name in the box above the output.

interface or type?

Both styles describe the same shape, so pick what your codebase already uses. An interface can be extended with extends and merged with another declaration of the same name; a type alias can also hold unions and mapped types. For API responses either works. When the JSON's top level is an array, the output always includes export type Root = RootItem[], since an interface cannot describe an array on its own.

Numbers, null and unknown

TypeScript has one number type, so whole numbers and decimals both become number. A key that is null in every sample becomes null, because there is nothing to tell what it holds when set; replace it with the real type once you know it. Empty arrays become unknown[], and arrays that mix kinds of values, like [1, "a"], become unknown[] too, so the compiler makes you check before use. Keys that are not valid identifiers, such as first-name, are kept as quoted property names, so nothing is renamed.

Types are not runtime checks

These types exist only while compiling. Writing const data = (await res.json()) as Root tells the compiler what to expect but checks nothing; if the API changes, the code compiles and fails later. Base the types on a few real responses, including ones with empty lists and missing fields, and validate data from sources you do not control.

All json tools