JSON to TypeScript
Infer TypeScript type definitions from JSON
What is JSON to TypeScript?
A JSON to TypeScript converter reads a sample JSON payload and infers the matching interface definitions, covering nested objects, array element types, and union types. It is a fast way to turn an API response into typed code, and the inference runs entirely inside your browser.
How to use JSON to TypeScript
- 1Paste a representative JSON response — the more complete the fields, the more accurate the inference.
- 2Enter a name for the root type; the default is Root.
- 3Review the generated interfaces; nested objects are extracted into separate named types.
- 4Copy the result into your project, then mark optional fields by hand according to your API contract.
How do I do this in code?
Use the tool above for one-off work; for anything you repeat, move it into a script or your project.
interface Root {
id: number;
name: string;
tags: string[];
owner: RootOwner;
}
interface RootOwner {
id: number;
email: string;
}Common errors and how to fix them
| Symptom | Cause | Fix |
|---|---|---|
| Array elements are inferred as unknown[] | The array is empty in the sample JSON, so there is no element to infer a type from. | Use a sample that contains at least one element, or fill in the element type by hand. |
| A field is inferred as the null type | That field happened to be null in the sample, which reveals nothing about its real type. | Widen it to a union such as string | null based on your API contract. |
Frequently asked questions
Can I use the generated types in production code?+
Treat them as a starting point that still needs review. A single JSON sample cannot express optional fields, unions, or what a null value really means, so compare the output against your API documentation and add ? and union types where they belong. The durable answer is generating types from an OpenAPI or GraphQL schema.
What happens when a field name is not a valid identifier?+
Keys with hyphens or spaces, or keys that start with a digit, are emitted as quoted property names such as "user-id": string. That is valid TypeScript; you just have to read those properties with bracket notation.
Related tools
All toolsJSON to YAML Converter
Convert JSON to YAML while preserving the nesting
YAML to JSON Converter
Convert YAML to JSON, multi-document files included
JSON to CSV Converter
Convert a JSON array of objects to CSV, with nested fields flattened automatically
CSV to JSON Converter
Convert CSV to JSON with automatic header and delimiter handling
JSON to Go Struct
Convert JSON to Go structs with json tags filled in
JSON to Python Dataclass
Convert JSON to Python dataclasses or Pydantic models