JSON to TypeScript Converter

Convert JSON data into TypeScript declarations with RomitJSON. Generate a starting point for API responses, configuration objects, and other structured data, then review the inferred types for your project.

Use a valid TypeScript identifier. Arrays of objects are merged as supplied samples; inferred optional fields reflect only those samples.

Input JSON

0 characters

TypeScript output

Generated TypeScript appears here.

Paste JSON or open a file to begin.

How inference works

Arrays of objects are treated as examples of one shared shape. Fields missing from any supplied object are marked optional when that setting is enabled; explicit null remains nullable. Empty arrays use the selected fallback. Inference describes only the JSON you provide and cannot guarantee every runtime value.

Generate TypeScript Types from JSON Samples

Use this JSON to TypeScript converter to turn a representative JSON object or array into TypeScript declarations. It can save the repetitive first pass when you are describing an API response, a configuration file, a webhook payload, or a test fixture. The result is a draft based on the values you provide, so compare it with the data contract before relying on it in an application.

Paste JSON or open a local .json file, choose a root name and declaration style, and select Generate types. The tool builds types for nested objects and arrays, then lets you copy the result or download it as types.ts. Conversion runs in your browser; the page does not upload the JSON to a conversion service.

How to Use the JSON to TypeScript Converter

  1. Paste valid JSON into the input panel, or select Open JSON to choose a file.
  2. Set a root type name such as Person. It must be a valid TypeScript identifier.
  3. Choose Interfaces or Type aliases, then set export, optional-property, readonly, and empty-array options as needed.
  4. Select Generate types. Check the output against representative values and your API or application contract.
  5. Copy the declarations or download the generated .ts file.

For the most useful inference, provide examples that show meaningful variation in your data. In particular, include multiple objects when some records may omit fields.

Optional and Nullable Fields Are Different

This distinction matters when modeling API data. An optional property may be absent from an object; a nullable property is present with the value null. A field can be both optional and nullable. The converter can infer these cases from an array of sample objects when Infer optional fields is enabled.

Sample JSON

[
  { "id": 101, "name": "Ada", "active": true },
  { "id": 202, "name": "Grace", "active": false, "nickname": null },
  { "id": 303, "name": "Katherine", "active": true, "nickname": "Kat" }
]

With the root name set to Person, the generated declarations include:

export interface PersonItem {
  id: number;
  name: string;
  active: boolean;
  nickname?: null | string;
}

export type Person = PersonItem[];

nickname? reflects that the first sample object has no nickname property. The null | string union reflects the values where the property is present. If you provide only one object, the converter cannot infer that other properties might be absent; it only sees that one example.

What the Converter Infers

JSON sampleTypical TypeScript resultWhat to check
"Ada"stringString formats such as dates remain strings.
101numberJSON numbers do not distinguish integers from decimals.
truebooleanBoth JSON boolean values use this type.
nullnull or a union containing nullNull is a value, not the same as an absent property.
["read", "write"]string[]An empty array needs an element-type fallback.
{ "city": "Patna" }A named object declarationNested object declarations are named from their property.

If sample values for a field have different JSON types, the generated declaration may use a union. Review each union to make sure it represents allowed data rather than inconsistent or accidental input.

Choose Interfaces, Type Aliases, and Generation Options

Select Interfaces when you want object declarations in the familiar interface form. Choose Type aliases if that style better matches your project. Both can describe object shapes; aliases also naturally express unions and primitive types. The tool may use a type alias for the root when the JSON root itself is an array or another non-object value.

Use Generated Types as a Starting Point

JSON can show what happened in one response, but it cannot reveal every value a service may return. A sample with an empty array cannot tell you its eventual element type. A single number cannot express business rules such as “positive integer,” and a date string is still inferred as string. Confirm these constraints from the API documentation, schema, or application requirements and refine the declarations where needed.

TypeScript declarations help tools check code during development, but they do not validate untrusted JSON at runtime. If an application needs to reject malformed API data, add a runtime validation step or use a schema and a compatible validator. Use the JSON Schema Generator to create a schema draft from sample JSON, then review its rules before using it as a contract.

Frequently Asked Questions

Can I convert a JSON array to TypeScript?

Yes. Arrays of values become TypeScript array types. For an array of objects, the converter merges the supplied records into one item shape and can mark properties missing from some records as optional.

How can I infer optional properties?

Include multiple representative objects in an array, with the optional property absent from at least one object, and leave Infer optional fields enabled. The inference only reflects the samples you provide.

Why does a property have both ? and null?

The question mark means the property may be absent. The null union means that, when present, its value may be null. These describe different cases.

What type does an empty JSON array get?

An empty array contains no evidence about its item type. This converter uses the selected fallback, which is unknown by default or any.

Does this converter validate future API responses?

No. It parses the JSON sample and generates static TypeScript declarations. Those declarations do not check future data at runtime; add runtime validation when your application needs it.

Is my JSON uploaded?

No. This page parses and converts the input in your browser. For sensitive data, follow your organization’s data-handling rules and review the privacy information.

Related JSON Tools

Use the JSON Formatter to make a sample easier to inspect, the JSON Validator to check its syntax, and the JSON Viewer to explore nested values. For other outputs, try the JSON Schema Generator, JSON to CSV Converter, JSON to YAML Converter, or JSON to XML Converter.