JSON Toolbox LogoJSONToolbox

JSON to TypeScript Converter

Generate TypeScript interfaces or type aliases from JSON data online. The converter infers objects, arrays, primitive types, nullable values, and nested structures, then produces editable TypeScript declarations that you can copy or download for API responses, configuration data, and frontend projects.

Generated TypeScript types will appear here...
All processing happens in your browser. No JSON is sent to our servers.

Key Features

Generate TypeScript Types

Infer TypeScript declarations from representative JSON data, including strings, numbers, booleans, null, arrays, objects, and nested structures.

Nested Interfaces and Arrays

Nested objects can become separately named interfaces or type aliases, while arrays are represented with typed item declarations such as string[] or User[].

Interface or Type Alias Output

Choose interfaces or type aliases when the option is available. Interfaces are useful for object contracts, while type aliases are better suited to unions, intersections, and other composed types.

Optional and Nullable Fields

Configure how the generator represents optional properties and null values. These choices are inferred from the sample and should be reviewed against the actual API or data contract.

Common Format Hints

Optionally detect common string patterns such as email, URL, UUID, date, or date-time when supported. These hints may be emitted as comments or selected type output depending on the implementation.

Copy TypeScript Code

Copy the generated declarations to your clipboard and place them in a .ts file, API model file, frontend type module, or project documentation.

Download a .ts File

Download the generated TypeScript source as a .ts file. Review names, optionality, nullability, unions, and array item types before importing it into your project.

Browser-Based Generation

JSON parsing and TypeScript generation happen in your browser. The page does not require an account or upload the input data to a remote service.

How to Use

1

Enter representative JSON

Paste a JSON object or array of objects, or open a local JSON file if file input is available. Use representative data that includes important optional fields and variations.

2

Choose the output style

Select interfaces or type aliases, set the root type name, and decide whether generated declarations should include export.

3

Configure nullability and optional fields

Choose how null values and optional properties should be represented. A field being nullable is not automatically the same as a field being optional.

4

Generate and review

Click Generate TypeScript and inspect nested names, array item types, unions, optional markers, null types, and property names that required quoting or sanitization.

5

Copy or download

Copy the TypeScript code or download a .ts file. Refine the generated declarations and add runtime validation when the input comes from an external API.

Input
{
  "id": 1,
  "name": "Alice",
  "email": "alice@example.com",
  "isActive": true
}
Output
export interface RootObject {
  id: number;
  name: string;
  email: string;
  isActive: boolean;
}

How to Generate TypeScript Interfaces from JSON

What Does JSON to TypeScript Mean?

JSON to TypeScript conversion generates static type declarations from representative JSON data. The result may use interfaces, type aliases, arrays, unions, optional properties, and nullable types to describe the shape observed in the input.

Generated declarations reduce repetitive typing work, especially for API responses and nested payloads. They are a starting point rather than a complete guarantee that all future runtime data will match the sample.

Common Use Cases

  • API response models: Create an initial type definition for data returned by a REST or GraphQL endpoint.
  • Frontend development: Add types to fetch, Axios, Vue, React, Angular, or other application code.
  • Configuration data: Describe the structure of JSON configuration or feature-flag files.
  • Test fixtures: Generate types for mock data, snapshots, and test payloads.
  • Database exports: Create a starting model from JSON records exported from a data system.

How to Generate TypeScript from JSON

  1. Provide representative JSON: Paste an object, array, or API response sample that includes the fields and variations that matter.
  2. Name the root type: Use a meaningful name such as UserResponse, Product, or ApiResult instead of leaving the generic RootObject name.
  3. Choose the output style: Select interfaces or type aliases and decide whether to add export.
  4. Configure optionality: Choose how optional fields and null values should be represented, then compare the result with the real data contract.
  5. Generate: Create the TypeScript declarations and review nested names, arrays, unions, and property names.
  6. Copy or download: Put the code in a .ts file and refine it before using it throughout the project.

Basic JSON to TypeScript Mapping

  • String: Becomes string.
  • Number: Becomes number; TypeScript does not distinguish integer and floating-point numbers in the usual type syntax.
  • Boolean: Becomes boolean.
  • Null: May require a union such as string | null when a field has both string and null values.
  • Object: Becomes an interface or type alias with properties.
  • Array: Becomes an array type such as User[] or Array<User>.

Nested Objects and Arrays

Nested objects are usually emitted as separate named declarations:

interface User {
  profile: Profile;
}

interface Profile {
  displayName: string;
}

Arrays require additional inference. If every object has the same shape, a single item interface may be sufficient. If objects differ, the generator may need optional fields or a union type such as Admin | Member. Review the output when arrays contain multiple shapes.

Optional Fields and Nullable Fields

Optional and nullable mean different things:

  • Optional: email?: string means the property may be absent.
  • Nullable: email: string | null means the property is present but may contain null.
  • Both: email?: string | null means the property may be missing or explicitly null.

One JSON example cannot prove whether a missing field is optional. Use multiple samples or confirm the API contract before choosing the final declaration.

Interfaces and Type Aliases

Interfaces are often convenient for object-shaped models and can participate in declaration merging. Type aliases can represent primitives, unions, intersections, tuples, and mapped types. A generator that defaults to interfaces is useful for common object payloads, but type aliases may be a better output for mixed arrays or union-heavy data.

TypeScript Types Do Not Validate Runtime JSON

A TypeScript declaration helps the compiler and editor understand code, but it is not automatically a runtime validator. Data loaded from an API still needs validation if it can be malformed, incomplete, or controlled by another system. Use JSON Schema, Zod, Valibot, io-ts, typia, or another runtime validation approach when the application requires runtime guarantees.

String Formats Are Still Strings

An email address, URL, UUID, date, or IP address is normally still a string in TypeScript. A generator can add comments, branded types, or validation metadata when supported, but pattern detection alone does not validate future values.

Property Names and Naming Rules

JSON keys may contain characters that are inconvenient or invalid for ordinary TypeScript identifier syntax:

{"first-name":"Alice","user id":42}

These properties may need quoted keys, such as "first-name": string, or a configured naming transformation. Renaming keys in the generated type does not rename the keys in the runtime JSON object, so use a separate transformation when the data shape must change.

Use More Than One Example

One sample often misses optional fields or alternate response shapes. If your API returns different variants, collect representative examples and merge them before generating types, or use a tool that accepts multiple files or samples. Then review which properties are always present, which are optional, and which require a union.

Keep Generated Types in Sync

Generated types can drift from the API when the backend changes. Regenerate them from updated fixtures or schema definitions, and add runtime validation or contract tests where correctness matters. Treat generated code as part of the development workflow rather than a permanent substitute for an API contract.

Browser-Based Privacy

This converter is designed to parse the JSON and generate TypeScript in the browser instead of sending input to a remote conversion service. That can be convenient for development samples and private configuration snippets, but it is not a secure secret-management system. Avoid entering credentials, tokens, private customer records, or production secrets into any online tool.

Related Articles

Related Tools

Frequently Asked Questions

What does JSON to TypeScript conversion generate?

The converter generates TypeScript interfaces or type aliases that describe the observed JSON structure. The output is a static type declaration; it does not validate runtime data by itself.

Does it generate interfaces or type aliases?

The output depends on the selected mode. Interfaces are commonly used for object-shaped contracts, while type aliases can represent unions, intersections, primitive aliases, and more complex compositions.

Does the generated TypeScript validate API responses at runtime?

No. TypeScript types are removed during compilation and do not validate unknown runtime data. If API data must be checked at runtime, use a validator or schema-based library such as Zod, Valibot, io-ts, or JSON Schema validation.

How are nested JSON objects handled?

Nested objects can be emitted as separate named interfaces or type aliases and referenced from the parent declaration. Generated names are inferred from property names and may need manual refinement.

How are JSON arrays handled?

Arrays are represented with TypeScript array types such as string[], number[], or User[]. If an array contains multiple object shapes or incompatible value types, the result may require a union type or manual editing.

Can I generate types from a JSON array?

Yes. The generator can create an array type and infer the item declaration. For an array of objects, it may generate a root array type that references a named item interface.

What is the difference between optional and nullable?

An optional property may be absent, such as name?: string. A nullable property is present but may contain null, such as name: string | null. A field can be both optional and nullable when both states are valid.

Can one JSON example determine optional fields?

No. A single example cannot prove that a field is optional or required. The generator can use configured heuristics, but you should compare multiple representative responses or confirm the API contract manually.

How are null values represented?

When null preservation is enabled, a field may become a union such as string | null or a null type. The correct result depends on whether null is a valid value in the actual data contract.

What happens when array items have different shapes?

The generator may merge compatible object fields, mark some fields optional, or produce a union of object types. Review mixed arrays carefully because a single generated interface may be too broad or too strict.

Does it detect dates, email addresses, or URLs?

The generator can optionally recognize common string patterns, but TypeScript normally still represents these values as string. A detected format may be emitted as a comment or affect an optional output mode; it does not create a runtime-branded type unless explicitly implemented.

Can JSON keys be used directly as TypeScript property names?

Many JSON keys are valid TypeScript identifiers, but keys containing spaces, hyphens, leading digits, or reserved words may require quoting or sanitization. Review generated property names before using the output.

Can I customize interface names?

Yes. Set the root type name, and use the generated nested names as a starting point. Nested names may need manual changes when property names are generic, abbreviated, or reused in different contexts.

Is the generated file ready to import into my project?

It is valid starting TypeScript when the input and generator output are valid, but you should review naming, optionality, nullability, unions, and array rules before treating it as a stable project contract.

Can I use generated types instead of API validation?

No. Static TypeScript declarations help during development but do not protect your application from malformed or unexpected runtime JSON. Add runtime validation when data comes from a network, user input, files, or another untrusted source.

Is my JSON uploaded to a server?

This page is designed to parse JSON and generate TypeScript in your browser without requiring a server upload. You should still avoid entering passwords, API keys, access tokens, private customer data, or confidential production payloads into any browser-based tool.

Is this a free JSON to TypeScript converter?

Yes. You can paste or open JSON, generate TypeScript declarations, and copy or download the result without creating an account.