What the converter does
When you call an API, you usually get JSON and need TypeScript types for it. Writing them by hand is slow for large responses. This tool reads a JSON sample and generates TypeScript interfaces (or type aliases) for it, including nested objects, arrays and fields that are sometimes missing.
How to use it
- Paste a JSON sample, ideally a real response.
- Set the name of the root type.
- Choose interface or type, and whether to add export.
- Optionally mark null fields as optional.
- Copy the generated code into your project.
How the types are inferred
- Strings, numbers and booleans become string, number and boolean.
- Nested objects become their own named interfaces, named after the property.
- Arrays become Type[]. If array items have different shapes, they are merged and fields missing from some items become optional.
- A null value becomes null. Mark it optional if the field can also be absent.
- Property names that are not valid identifiers are quoted.
Example
A sample with two order records:
{ "id": 1, "name": "Asha", "orders": [{ "id": 7, "coupon": "NEW" }, { "id": 8 }] }
→
export interface Root {
id: number;
name: string;
orders: Order[];
}
export interface Order {
id: number;
coupon?: string;
}Important limits
The types come from your sample only. If a field can also be a number in some responses but your sample shows a string, the type will be too narrow. Use a sample with several records, check the generated code against your API documentation, and treat the result as a starting point.
Related tools
- JSON Formatter & Validator: Format, validate, and beautify JSON with clear error messages for developers.
- YAML Validator & Formatter: Validate YAML with line and column errors, reformat it, sort keys and convert between YAML and JSON.
- JSON to CSV & CSV to JSON Converter: Convert JSON arrays to CSV and CSV to JSON with quotes, nested objects and custom delimiters handled.
- Code Beautifier & Formatter: Format JavaScript, TypeScript, JSON, CSS, SCSS, HTML, Markdown and YAML with the Prettier engine.
