JSON to TypeScript

Turn a JSON sample into a TypeScript interface, a starting type for an API response. It infers types from that one sample, so refine the result by hand.

Developer Tools● Free No upload Instant

Loading JSON to TypeScript…

Your browser is preparing the tool. It runs 100% locally.

Quick answer

Paste a JSON sample and the tool generates a matching TypeScript interface, reading each value to pick number, string, boolean, an array type or a nested object. It is a fast way to get a starting type for an API response. Because it works from a single sample, every field comes out required, and it cannot spot optional or union types, so treat the result as a first draft. It all happens in this tab, and nothing is uploaded.

What the JSON to TypeScript does

This tool reads a JSON object and writes a TypeScript interface that describes its shape, assigning each property a type based on its value and turning nested objects into nested types. The output is ready to paste into your code as a starting point.

It saves hand-writing interfaces for API responses and config objects: give it a representative sample and it returns a typed skeleton.

How it works

The tool parses the JSON, then walks each property and infers a type from its value: a number becomes number, text becomes string, true or false becomes boolean, an array becomes a typed array, and a nested object becomes a nested interface. It assembles these into an interface it names Root.

Everything is inferred from the one sample you provide. That makes it fast, but it also means the result reflects only what is in that sample: the tool sees one example of each field and types it accordingly, with no knowledge of how the data varies across other records.

Data source & conversion logic

  1. Parse the JSON. Read the sample into a data structure, which also checks it is valid JSON.
  2. Infer each field's type. Map each value to a TypeScript type: number, string, boolean, array or nested object.
  3. Type the arrays. Take an array's type from its first element, or any for an empty array.
  4. Assemble the interface. Combine the typed fields into an interface named Root, ready to copy.

How types are inferred

number → number text → string true/false → boolean array → (type of first element)[] object → nested interface null → any every field is required (no optional ? from a single sample)
Worked example
{ id: 1, name: x, active: true, tags: [a] } → interface Root { id: number; name: string; active: boolean; tags: string[]; }

Inferred from one sample, so every field is required and arrays take the type of their first element. Optional fields and unions need multiple samples, which this tool does not take.

Privacy

Your JSON never leaves your device. The interface is generated in your browser with no network request, so nothing is uploaded.

Because the work is local, you can generate types from real API payloads or internal data without it being stored or sent anywhere.

The tool keeps nothing between sessions: close the tab and the JSON and the generated type are gone.

Standards & references

  • TypeScript interfaces: An interface describes the shape of an object: its property names and their types. Generating one from JSON gives you compile-time checking for data that arrives as plain JSON at runtime.
  • Single-sample inference: Types are inferred from one example. That cannot reveal which fields are sometimes absent (optional) or which hold more than one type (unions). That only shows up across several varied samples.
  • Multiple samples for richer types: Tools like quicktype take several JSON samples: a field missing from some becomes optional, and a value with different types becomes a union. Three to five diverse samples give the most accurate types.

Accuracy and limits

For a representative sample, the interface is a correct starting type: the field names and basic types match the data, ready to drop into your code and refine.

Every field comes out required. Because it sees one sample, the tool cannot know which properties are optional, so a field that is sometimes missing in real data will still be typed as always present. Add the optional marker by hand where needed.

It cannot infer unions. A field that is a number in one record and a string in another, or an array of mixed types, is typed only from this sample. Arrays in particular take the type of their first element, so a mixed array is typed wrongly and an empty array becomes an array of any.

Nested objects become inline anonymous types rather than separate named interfaces, a null value becomes any, and everything is named Root. For a polished set of named, optional and union types, generate from multiple samples with a dedicated tool.

Where people use it

Typing an API response

Get a starting interface from a sample response payload.

Stop hand-writing types

Generate a typed skeleton instead of typing each field.

Modelling config objects

Turn a JSON config into a TypeScript shape.

Learning TypeScript types

See how JSON values map onto TypeScript types.

When it fits, and when it doesn't

Good for

  • A quick starting interface from a sample
  • Typing a straightforward API response
  • Turning a JSON config into a type
  • Learning how JSON maps to TS types

Not the best choice for

  • Optional fields (everything comes out required)
  • Union types or mixed-type arrays
  • Named types for nested objects
  • Production types without a manual review

For optional fields, unions and named nested types, use a generator that accepts multiple samples (such as quicktype) and feed it several diverse records. Always review generated types and add the optional markers and unions your data needs.

Frequently asked questions

Why is every field required?
Because the tool infers from a single sample, it cannot tell which fields are sometimes absent. Everything present in your sample is typed as required. Add the optional marker by hand for fields that may be missing.
Can it detect optional or union types?
No. Optionals and unions only become visible across several varied samples: a field missing from some, or a value with different types. From one sample, the tool types each field as it appears.
How are arrays typed?
From the first element. So an array of strings becomes string array, but a mixed array is typed only from its first item, and an empty array becomes an array of any. Check array types if your data varies.
What happens to a null value?
It becomes any, because a null on its own gives no information about the real type. If you know the intended type, set it by hand after generating.
Does it name nested objects?
No. Nested objects are typed inline as anonymous shapes rather than separate named interfaces. For larger structures you may want to extract and name them yourself.
Is the output valid TypeScript?
Yes. It produces a valid interface you can paste straight into your code. It is a correct starting type; refining optionals and unions is the part left to you.
How do I get better types?
Provide multiple, diverse JSON samples to a generator built for that, like quicktype. With several samples it can mark optional fields and infer unions, producing far more accurate types than a single sample allows.
Does it validate my JSON?
It parses the JSON to read it, so invalid JSON will fail rather than generate a type. A successful result means the sample was valid JSON.
Why is the interface named Root?
It uses a fixed top-level name. Rename it to suit your data. The name does not affect the types, only how you refer to the interface.
Can it generate types instead of interfaces?
It outputs an interface. You can convert it to a type alias by hand if you prefer, since the two are largely interchangeable for object shapes in TypeScript.
Is my JSON uploaded anywhere?
No. Your browser generates the interface and sends nothing to a server, so real payloads stay on your device.
Does it handle deeply nested JSON?
Yes, it recurses into nested objects and arrays. Very deep or large structures produce correspondingly large inline types, which you may want to break into named interfaces by hand.

References

Infers a TypeScript interface by reading the types in one JSON sample. Single-sample inference makes every field required and cannot detect optional or union types.

PopularHot

JSON Formatter

Pretty-print messy or minified JSON with 2-space indents.

DeveloperOpen
Popular

JSON Validator

Paste JSON and find out whether it parses.

DeveloperOpen
Popular

Base64 Encoder

Standard Base64 for any text, emoji included.

DeveloperOpen
Popular

Hash Generator

MD5, SHA-1 and the SHA-2 hashes of any text, as you type.

DeveloperOpen
Popular

UUID Generator

Up to 500 random version 4 UUIDs at once.

DeveloperOpen
Trending

CSS Minifier

Strips comments and whitespace to put a stylesheet on one line.

DeveloperOpen

Back to the JSON to TypeScript

Paste, run and copy. The code stays in your browser and you don't need an account.