JSON to TypeScript
Paste a sample JSON response and get ready-to-use TypeScript interfaces. Nested objects become their own interfaces, array items are merged so missing keys become optional, and mixed values become unions.
Runs in your browser. Your files are never uploaded.
Loading JSON to TypeScript…
How to use JSON to TypeScript
- Paste a representative JSON sample into the left box, or press Load sample.
- Choose a root type name, and whether to declare with interface or type.
- Tick or untick export depending on whether you want the declarations exported.
- Copy the generated TypeScript into your project.
Why generate types from JSON
TypeScript catches mistakes at compile time, but only when it knows the shape of your data. Data from a REST API, a configuration file or a third-party webhook arrives as untyped JSON, and writing interfaces for large responses by hand is slow and error-prone. Generating a first draft from a real sample gives you autocomplete and type checking in minutes.
This generator infers types directly from the structure of the JSON you paste. It runs entirely in your browser, so it is safe to use with real responses that contain private information.
How inference works
Primitive values become string, number or boolean. Objects become named interfaces, with the name taken from the key that contains them (for example, a key called orders holding objects produces an Order interface). Arrays are scanned item by item and merged into one element type.
- A value that is null in some places and a string in others becomes string | null.
- Arrays holding different kinds of values become unions such as (string | number)[].
- Keys that are not valid identifiers, such as "first-name", are quoted.
- If two nested objects would get the same name, a number is added to keep names unique.
- A top-level array or primitive produces a type alias.
Use it as a starting point
Treat the output as a draft. Rename interfaces to match your domain, tighten string fields to literal unions where you know the allowed values, and replace numbers that are really identifiers or dates with more specific aliases. For strict runtime safety, pair your types with a validation library, because TypeScript types are erased at runtime and do not check incoming data.
Also remember that one sample cannot reveal everything. Optional fields, rarely populated arrays and fields that are sometimes null may not appear in a small sample. Paste an array that combines several records to let the tool merge them and mark optional keys correctly.
Why use this json to typescript tool
- Nested interfaces. Each nested object gets its own named interface, named from its key and de-duplicated.
- Smart array merging. All items in an array are merged: keys missing from some items become optional, and differing value types become a union.
- null and union types. A field that is sometimes null becomes string | null, for example.
- Interface or type. Output either interface declarations or type aliases, with optional export.
Frequently asked questions
How does it decide which keys are optional?
Within an array of objects, a key that appears in only some items is marked optional with a question mark. A key present in every item is required. For a single object, everything is required, because one sample cannot show what is optional.
Is the generated type guaranteed to match my API?
No. A type inferred from a sample reflects only that sample. A field that was always a string in your sample might be null or a number elsewhere. Use several representative samples, or a combined one, and review the result, ideally against the API documentation or schema.
How are empty arrays handled?
An empty array has no items to learn from, so its type becomes unknown[]. Replace it with the real type once you know it, or paste a sample that contains at least one item.
More free tools
JSON Formatter
Format, validate, beautify and minify JSON instantly. Shows the exact line and column of any error. Free and runs in your browser.
Open toolJSON to CSV
Convert a JSON array to CSV in your browser. Flattens nested objects, picks delimiters and downloads a ready .csv for Excel or Sheets.
Open toolCSV to JSON
Convert CSV to JSON in your browser. Auto-detects delimiter, uses the header row as keys, and can convert numbers and booleans.
Open tool