JSON to TypeScript Guide: Inferring Types From a Sample
Learn how objects, arrays and primitives map to TypeScript types, and where inference needs your judgement.
What is the JSON to TypeScript Converter?
Paste a JSON sample and this tool walks through it recursively: objects become interfaces, arrays become element types, and strings, numbers and booleans become their matching types, with nested objects getting their own interfaces. It is a starting point for typing an API response, so check the result against the real data, especially for fields that are sometimes missing or sometimes null. Invalid JSON produces a clear error instead of a guess.
How to use it
- Paste your JSON sample into the box.
- Click Convert.
- Copy the generated interfaces into your TypeScript code and adjust optional fields as needed.
Worked example
For the sample, the tool produces interface Root with name: string, age: number, active: boolean and tags: string[], each on its own line.
Why is a sample not a full type definition?
Because a sample shows only the values present in that one example. Fields that are optional, nullable or vary in shape need a human to mark them as such.
Frequently asked questions
What happens with an empty array?
It becomes unknown[], because an empty array gives no clue about its element type. Replace it once you know what the array holds.
Are nested objects handled?
Yes. Each nested object gets its own interface, named from its field, and the parent refers to it by that name.