JSON to TypeScript Converter

Paste JSON and get TypeScript interfaces or types. Nested objects, arrays, null values and optional fields are handled for you.

Your JSON is not uploaded. The conversion runs locally in your browser. Nothing is sent to a server, logged or stored.

How to use the JSON to TypeScript converter

  1. Paste a JSON object or array, for example a real API response, into the input box.
  2. Name the root type, choose interface or type, and set the export and readonly options.
  3. Copy the result or download it as a .ts file and drop it into your project.
  4. If the JSON is rejected, check it with the JSON Validator. Starting from a spreadsheet? Convert it first with CSV to JSON.

Worked example: optional fields and nested types

The sample order above has two line items. The first has sku, qty and price; the second also has giftNote. Because giftNote is missing from one element, it becomes optional. The other rules at work:

  • Nested objects become their own named types, named after the key: customer becomes Customer.
  • Arrays of objects become a singular type name: items becomes Item[].
  • phone: null becomes phone: null. If another sample held a string there, you would get string | null.
  • Numbers (integer or decimal) are number; tags becomes string[].
export interface Item {
  sku: string;
  qty: number;
  price: number;
  giftNote?: string;
}

Types are inferred from the sample only. A field that never appears, or an enum-like string, cannot be detected, so review the output. TypeScript types are erased at compile time and do not validate data at runtime.

Frequently Asked Questions (FAQ)

Is my JSON uploaded anywhere?

No. The conversion runs entirely in your browser with JavaScript. Your JSON is not sent to a server, logged or stored, so it is safe to paste real API responses (though you should still remove secrets such as tokens before sharing the result).

How are optional fields detected?

When the JSON contains an array of objects, the converter merges every element. A key that is missing from some elements becomes optional (key?: type), and a key whose type differs between elements becomes a union such as string | number. A single object has no variation to compare, so all its keys are required.

How are null values handled?

A null value becomes the type null. If the same key is a string in one array element and null in another, the result is string | null. A key that only ever holds null is typed null, because a sample alone cannot tell what else it could be; edit it to the real type.

Should I use interface or type?

For plain object shapes they are interchangeable. interface can be extended and merged and gives clearer error messages; type aliases can also name unions and tuples. Pick the one your codebase already uses; the tool can output either.

Is the generated type guaranteed to match my API?

It matches the sample you pasted, nothing more. Fields that are absent from the sample, numbers that are sometimes strings and enums can’t be inferred from one example. Paste several representative responses in an array to get better optional and union detection, and treat the output as a starting point to review.

Does it validate the JSON at runtime?

No. TypeScript types exist only at compile time, so they do not check data coming over the network. For runtime checks use a schema library such as Zod, and validate your JSON structure first with the JSON Validator.

What happens with empty arrays and keys that are not valid identifiers?

An empty array has no elements to learn from, so it becomes unknown[]. Keys such as first-name or 2nd are quoted automatically ("first-name": string).