JSON to TypeScript

Paste JSON and get TypeScript interfaces or types: nested objects, optional fields, mixed arrays and nulls, with root name, export and readonly options.

About this tool

When an API returns JSON and your code is TypeScript, you need types that describe it, and writing them by hand for a nested response is slow and error-prone. Paste a JSON sample here and get ready-to-use interface or type declarations: nested objects become their own named types, arrays are merged into a single element type, fields missing from some array items become optional, and null values become unions. It runs entirely in your browser, so API responses containing real data are never uploaded.

How to use it

  1. Paste a representative JSON sample into the left box (or click Load sample to see how it works). Invalid JSON shows the parser error underneath.
  2. Set the root name (for example User or ApiResponse) and choose interface or type output.
  3. Toggle the options: optional fields for keys that are missing in some array items, export on each declaration, and readonly properties.
  4. Click Copy types and paste the result into your .ts file. Review it against the API documentation: a sample can only show what it contains.

FAQ

How does it handle arrays whose items have different shapes?

Every element of an array is merged into one type. If most objects have the keys id and name but only some have coupon, the generated type has coupon as an optional property (coupon?: string). If the same key holds different types in different items, the property becomes a union such as string | number. Arrays that mix primitives and objects become a parenthesised union array, for example (string | Item)[].

What happens with null values and empty arrays?

A null value becomes the type null, and when another item has a real value for the same key the result is a union such as string | null, with null always listed last. An empty array has no elements to learn from, so it is typed as unknown[]. Provide a sample with at least one element if you want a precise element type.

Should I choose interface or type?

For plain JSON object shapes they are interchangeable in practice. Interfaces can be extended and merged with declaration merging and give slightly friendlier error messages; type aliases are required for unions and can express things interfaces cannot. This tool generates the same structure either way, so pick whichever your codebase uses.

Are my JSON and the generated types sent anywhere?

No. Parsing and type generation both run in your browser with plain JavaScript, with no network request involved. That makes it safe to paste real API responses, though you should still avoid sharing secrets in code you commit.

Can the types be wrong?

Yes, because they are inferred from the one sample you provide. A field that happens to be a number in your sample may be a string in other responses, and a field that is always present in your sample may be optional in the API. Feed it a sample that covers the variations, or check the result against the API specification or a schema.

How are keys that are not valid identifiers handled?

Keys such as "content-type" or "2fa" are quoted in the generated property list, and names for nested types are built from the key in PascalCase (an orders array produces an Order type). If two different shapes would get the same name, a number is appended, and identical shapes are reused under a single name.

What is the difference between this and the Data Format Converter or JSON Tools?

JSON Tools formats and validates JSON, and the Data Format Converter changes the data between JSON, YAML, XML and other formats. This tool does neither: it reads the structure of your JSON and writes TypeScript declarations that describe it.

Related tools

Guides & comparisons

← All tools · Blog · Cheat sheets · FAQ