Convert JSON to TypeScript Interfaces
JSON to TypeScript
NEVER LEAVES YOUR BROWSER
Paste one JSON document and get an export interface for every object in it. Keys missing from some siblings become optional, arrays of objects become an interface for the element type, and values that disagree become a union.
JSON input
TypeScript
Options
What the generator can and cannot tell
- Optional keys come from absence. A field present in every sampled object stays required; one missing from any sibling gets
?. That is the only inference JSON supports, so give it an array of several real records rather than one. - It cannot see a field that never appears. A key every element omits is absent from the interface, not optional. Two documents with different shapes produce two interfaces, not one wider one.
- Nothing here is validated against your code. String-literal unions, branded IDs and
Datefields are judgements only you can make. This gives you the shape your sample implies; you edit it.
More from JSONBeautify
JSON Beautifier & Formatter
Paste JSON to pretty-print it with 2 or 4 spaces, or minify it.
JSON Diff Comparator
Paste two JSON documents to see every value that was added, removed or changed, each listed by its JSON…
JSON Syntax Validator & Error Locator
Locate precise line and column syntax errors in malformed JSON files and automatically repair trailing…
Guides
Frequently Asked Questions
Why are some fields optional in the generated interface?
When an array holds objects and a key appears in only some of them, the generator marks it optional with a question mark. For [{"id":1,"name":"Ada"},{"id":2}], id is required and name is optional, because the second object has no name.
What type does an empty or mixed array get?
An empty array becomes unknown[], because there is nothing to infer from, so replace it with the real element type. An array that mixes types becomes a union: [1, "x"] gives (number | string)[]. Keys that are not valid identifiers, such as first-name, are quoted.
Developer Sponsor / Partner