About this tool
Writing model classes by hand for a large API response is slow and error-prone. This generator reads a JSON sample, works out the type of every field, merges the items of an array into one type, and writes the matching code. Fields missing from some items become optional, and null values become nullable types.
Example output
The same JSON sample produces idiomatic code in each language. For TypeScript, a user object with an optional manager looks like this.
export interface User {
name: string;
age: number;
roles: string[];
manager?: any;
}Review the generated types
The generator only sees the sample you give it. A number that is always whole in your sample is typed as an integer, even if the API can also return decimals, so add a more varied sample when in doubt. Property names are converted to each language's conventions, and the original JSON name is kept in an attribute or alias.
How to use it
- Paste a JSON sample, ideally one that shows every field your data can contain.
- Choose a language and, if you like, a root class name and TypeScript style.
- Copy the code or download it as a file with the right extension.
Frequently asked questions
How are optional and nullable fields detected?
If a key is missing from some objects in an array it is optional. If a value is null in some items it is nullable. A single sample cannot show every case, so review the result.
Which libraries do the C# and Java models use?
C# uses System.Text.Json attributes. Java uses Jackson annotations. Python uses Pydantic v2.
What happens with mixed types?
A field that holds different types, such as a string in one item and a number in another, becomes any, unknown or object, depending on the language.