JSON to Types
Turn a JSON sample into TypeScript interfaces, Go structs, Python models, C# classes, Rust structs or a JSON Schema.
JSON to Types - TypeScript, Go, Python, C#, Rust and JSON Schema
Paste a JSON sample, such as a response from an API, and get the type definitions to go with it: TypeScript interfaces, Go structs with JSON tags, Pydantic models or dataclasses, C# classes, Rust structs for serde, or a JSON Schema. Nested objects become their own named types, so the result reads like code a person would write.
API responses often contain real customer data, tokens and internal IDs. The conversion runs entirely in your browser, so none of it is sent anywhere.
How to use it
- Paste JSON into the left box, or press Sample to see an example.
- Choose the Output language.
- Set the Root name of the top-level type; for TypeScript, choose
interfaceortype. - Copy the result into your project.
How the types are worked out
- Numbers become integers when every value is whole, and floating-point otherwise:
42and18.5in the same field give a float. - Arrays of objects are merged: every key seen in any item appears in the type.
- Optional fields: with Mark missing keys optional on, a key missing from some items becomes optional (
coupon?: string,omitempty,Option,= None). - Nullable fields: a value that is
nullin one item makes the field nullable, for examplestring | nullor*stringin Go. - Mixed types, such as a number in one item and a string in another, give
unknown,Anyorinterface{}, so you can decide by hand. - Names come from the keys:
addressgivesAddress, and the items ofordersgiveOrder. Objects with the same shape share one type.
Per language
| Output | What you get |
|---|---|
| TypeScript | export interface or export type; keys that are not valid names are quoted |
| Go | Exported structs with json tags, int64 and float64, and common initialisms such as ID and URL |
| Python (Pydantic) | BaseModel classes; snake_case fields with Field(alias=...) when the JSON key differs |
| Python (dataclass) | @dataclass classes, with optional fields placed last as dataclasses require |
| C# | Classes with [JsonPropertyName] for System.Text.Json, long, double and List<T> |
| Rust | Structs deriving Serialize and Deserialize, with #[serde(rename)] where needed |
| JSON Schema | A draft 2020-12 schema with required lists, ready for validators |
Get the most accurate types
A type is only as complete as the sample it came from. Use a response that includes every optional field at least once, and paste an array with several different items rather than one, so the merging can see which fields vary. Dates arrive in JSON as strings and stay strings; convert them in your code where you need real date types.
Related tools
To tidy or validate the JSON first, use the JSON formatter. To turn it into YAML, CSV or XML, use the JSON converter. The task pages open this tool with a language chosen: JSON to TypeScript, JSON to Go, JSON to Python, JSON to C# and JSON Schema generator.