Generating Types and Code From a JSON Sample
8 min read
When you integrate an API, you usually need typed models in your language: TypeScript interfaces, Go structs, Python dataclasses, Java records. Writing them by hand means transcribing every field and type by eye, which is slow and easy to get wrong. Generating them from a real JSON sample is far quicker β provided you understand what inference can and cannot know.
How type inference works
A generator reads your sample and maps each JSON value to a language type: strings to string, booleans to bool, objects to their own named type, arrays to a typed list. Nested objects become separate, reusable definitions rather than one giant inline blob. From this JSON:
{ "id": 10, "name": "Ada Lovelace", "active": true, "roles": ["admin"] }a TypeScript generator produces:
interface Root {
id: number;
name: string;
active: boolean;
roles: string[];
}The number problem
JSON has a single number type, so a generator cannot tell an integer from a float, or a 32-bit int from a 64-bit one. Different tools make different safe choices β Go defaults to float64, Java generators often use Double. Always review numeric fields and tighten them to int/long where your schema guarantees whole numbers.
Why you should paste an array, not one object
Optionality is the other thing inference cannot guess from a single object. If a field is absent from some records but present in others, the generator can only detect that if it sees multiple records. Paste a representative array of objects and fields that are missing in some elements are correctly marked optional (? in TypeScript, Optional[...] in Python). From one object, every field looks required.
Keys that are not valid identifiers
JSON keys like user-id or 2fa are not valid field names in most languages. Generators handle this by renaming the field and preserving the original key β Go uses a struct tag (json:"user-id"), and in other languages you add an annotation such as Jackson's @JsonProperty. Check these after generating, because a wrong tag means the field silently stays empty on deserialization.
Pick your target
- JSON to TypeScript β interfaces with optional fields.
- JSON to Go β structs with
jsontags. - JSON to Python β dataclasses or Pydantic models.
- JSON to Java β records, POJOs or Lombok classes.
For runtime validation rather than compile-time types, generate a JSON Schema from the same sample.
Frequently asked questions
Why are all my integers typed as floats or doubles?
Because JSON does not distinguish integers from decimals. The generator picks a type that cannot lose data, then leaves it to you to narrow it where you know the value is a whole number.
How do I get optional fields marked correctly?
Paste an array of objects rather than a single one. A field missing from any element is then detected as optional. A single object makes every field look required.
Is my JSON uploaded when I generate code?
No. All of these generators run entirely in your browser, so you can safely paste real API responses that contain tokens or personal data.
Related reading
To understand the format you are modelling, see What is JSON?; to validate the shape at runtime, read JSON Schema.
Primary references
This guide is checked against the specifications and runtime documentation below. Examples are also run through JSON Wallet before publication.
Keep reading
What Is JSON? A Beginnerβs Guide
JSON is the most widely used data format on the web. This guide explains what it is, how it is structured, and where you will run into it as a developer.
JSON Syntax Rules Explained (With Examples)
JSON has a small but strict grammar. This reference walks through every rule with valid and invalid examples so your data parses the first time.
Common JSON Errors and How to Fix Them
Every developer hits "Unexpected token in JSON" eventually. Here are the most common JSON errors, what causes them, and exactly how to fix each one.
Ready to put this into practice? Open the free JSON editor β