Skip to content

JSON to TypeScript converter

Paste a JSON sample and get TypeScript interfaces for every nested object, with optional keys and unions inferred from your data. Runs entirely in your browser.

TypeScript output

How to generate TypeScript types from JSON

  1. Paste a representative JSON sample, ideally a real API response with every field filled in.
  2. Set the root name, for example Order or User. Nested objects are named after their keys.
  3. Choose interface or type declarations, and whether to export them.
  4. Copy the output into a .ts file and tighten any string fields that only hold a few values.

For example, the JSON {"customer": {"id": 88, "email": "[email protected]"}} becomes:

export interface Root {
  customer: Customer;
}

export interface Customer {
  id: number;
  email: string;
}

How the types are inferred

Strings, numbers, and booleans map to string, number, and boolean. Every nested object gets its own named interface. Arrays are typed from all of their elements together, so an array of objects becomes one interface whose keys are optional when some elements leave them out, and an array of mixed values becomes a union array. Keys that aren't valid identifiers, like content-type, are quoted.

A single sample can only show the values it contains. If a field is null in your sample but holds a number elsewhere, paste an array with both cases and the result becomes number | null.

When to convert JSON to TypeScript

  • Typing the response of a third-party API that has no published types
  • Writing a first draft of models for a webhook payload
  • Typing fixtures and mock data in tests
  • Describing JSON columns stored in PostgreSQL or MySQL
  • Checking which fields an endpoint returns inconsistently

From payloads to dashboards

If the JSON you're typing lives in a database, Basedash can answer questions about it directly. Describe what you want to know in plain English, and Basedash writes the SQL, runs it, and builds the chart, across PostgreSQL, MySQL, BigQuery, Snowflake, and 750+ other data sources.

FAQ

How do I convert JSON to a TypeScript interface?

Paste a JSON sample, such as an API response, into the input panel. The converter infers a type for every key and prints TypeScript declarations as you type: one interface for the root object and one for each nested object, named after the key that holds it. Set the root name, choose interface or type aliases, and copy the result into your code.

How are arrays of objects typed?

The converter merges every element of an array into a single element type. Keys that appear in every element are required, and keys missing from some elements are marked optional with ?. The element interface is named from the singular form of the key, so a line_items array produces a LineItem interface. Arrays with different value types become union arrays, such as (string | number)[].

What happens with null values and empty arrays?

A key whose value is null in the sample is typed as null, and if another sample element has a real value for it, the result is a union such as number | null. An empty array has nothing to infer from, so it is typed as unknown[], and an empty object becomes Record<string, unknown>. Paste a fuller sample or edit those lines by hand to tighten them.

Should I generate interfaces or type aliases?

Both describe the same object shapes, so pick whichever your codebase uses. Interfaces can be extended and merged across declarations, which many teams prefer for API models. Type aliases are needed for unions and mapped types and read the same for plain objects. The converter's toggle switches every declaration at once, and the export toggle adds or removes the export keyword.

Does the converter detect dates, enums, or string formats?

No. JSON only has strings, numbers, booleans, null, objects, and arrays, so a timestamp like "2026-10-12T09:30:00Z" is typed as string and a status field is typed as string rather than a literal union. If a field only ever holds a few values, replace string with a union such as "paid" | "refunded" by hand after copying.

Is my JSON sent to a server?

No. Type inference runs entirely in your browser with JavaScript, so API responses, customer records, and tokens in your sample never leave your machine. Nothing is logged or stored, and the page keeps working offline once it has loaded.

Query your JSON data in plain English

Connect your data and describe what you want to see. Basedash handles the query, visualization, and dashboard.