ToolPick

JSON to TypeScript

Generate TypeScript interfaces from a JSON sample, with named nested interfaces and optional keys.

How it works

The JSON is parsed and each value is mapped to string, number, boolean or null. Every nested object becomes its own interface named after its key in PascalCase (an array under users gives User). Objects found in the same array are merged: a key missing from some of them becomes optional with ?, and a key whose type differs between them becomes a union. Arrays of different value types become a union such as (number | string)[], and an empty array is unknown[]. The result describes the sample you pasted, not every possible document: a field that is a string in your sample but sometimes a number elsewhere cannot be known.

Runs in your browser; nothing is uploaded.

FAQ

Why is a field typed as null?

The sample contained null and nothing else for that key, so null is all that can be inferred. Replace it with the real type, for example string | null. If another array item has a value, the type is a union such as string | null.

How are integers and dates typed?

JSON has one number type, so integers and decimals are both number. Dates are just strings in JSON, so they are typed string; convert them yourself if you parse them to Date.

What happens with keys that are not valid identifiers?

Keys such as "content-type" or "2fa" are written as quoted property names. Interface names are made from the key by dropping non-alphanumeric characters, prefixed with an underscore when they would start with a digit, and numbered (Address2) when two different shapes share a name.

Related developer tools

Bookmark this page for next time, or copy its link. Shared links contain no working input.