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
- Paste valid JSON into the input panel, or select Open JSON to choose a file.
- Set a root type name such as
Person. It must be a valid TypeScript identifier. - Choose Interfaces or Type aliases, then set export, optional-property, readonly, and empty-array options as needed.
- Select Generate types. Check the output against representative values and your API or application contract.
- Copy the declarations or download the generated
.tsfile.
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 sample | Typical TypeScript result | What to check |
|---|---|---|
"Ada" | string | String formats such as dates remain strings. |
101 | number | JSON numbers do not distinguish integers from decimals. |
true | boolean | Both JSON boolean values use this type. |
null | null or a union containing null | Null 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 declaration | Nested 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.
- Root type name: Sets the top-level declaration name. Nested declaration names are derived from object property names.
- Export declarations: Adds
exportso declarations can be imported from another module. - Infer optional fields: Marks fields absent from some objects in the supplied array as optional. Turn this off only when you intentionally want every observed field required.
- Empty array fallback: An empty array contains no element examples, so choose
unknownorany.unknownkeeps you responsible for narrowing the value before using it. - Readonly properties: Adds
readonlyto properties when the generated shape should not be reassigned through that type.
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.