Developer and Text Tools

Generate TypeScript, Go and Python Types from JSON Samples

Turn a sample API response into typed models for TypeScript, Go, Python, Java, C#, Rust, Kotlin or Swift, and see how optional fields are inferred.

Saizul Amin
Saizul Amin30 Oct 2026 · 3 min read
Generate TypeScript, Go and Python Types from JSON Samples

Generate TypeScript, Go and Python Types from JSON Samples

Writing model classes by hand for a large API response is slow and error-prone. You miss a field, guess a type wrong, or forget that one array item has an extra property. The free JSON to Code Types Generator reads a sample document and writes matching types for eight languages, keeping the original JSON names so serialisation works without surprises.

What it produces

Pick a language and a root type name, paste the sample, and you get:

  • TypeScript: interfaces with optional properties marked by a question mark and null in unions where a value was null.
  • Go: structs with json:"name" tags, omitempty on optional fields and pointer types where null appears.
  • Python: dataclasses with type hints, Optional for nullable fields and defaults for optional ones.
  • Java: records with Jackson property annotations.
  • C#: records with JsonPropertyName attributes for System.Text.Json.
  • Rust: structs deriving Serialize and Deserialize, with rename attributes and Option types.
  • Kotlin: data classes for kotlinx.serialization with SerialName.
  • Swift: Codable structs with a CodingKeys enum when names need mapping.

Nested objects become their own named types. The name comes from the key, so an address object becomes Address and an array called items becomes a list of Item.

How inference works

A single JSON value says little about a type, so the generator looks at everything the sample contains:

  • Numbers: a field that is always a whole number becomes an integer; if any sample is fractional it becomes a float or double.
  • Arrays of objects: every item is merged. A field present in some items but not others is optional. A field that is sometimes null is nullable.
  • Mixed values: a field that is a string in one item and a number in another gets the language's loose type, and you should decide what the API really means.
  • Empty arrays and nulls: with no example value, the type is unknown. TypeScript gets unknown, Python Any, Go the empty interface. Add a real example to the sample to get a precise type.

This is why a richer sample gives better code. Paste a response that includes optional fields and a few array items rather than the smallest possible example.

Names and conventions

JSON keys often use snake_case or kebab-case, while most languages prefer camelCase or PascalCase. The generator converts names to each language's convention and adds the mapping annotation, so created_at becomes createdAt in Kotlin with @SerialName("created_at"). Keys that are not valid identifiers, such as ones starting with a digit, are renamed safely and still mapped back to the original.

Dates, ids and money

JSON has no date type, so timestamps arrive as strings. The generator leaves them as strings, because guessing a date type would break on the first value in an unexpected format. Convert them in your own code with a proper date library. The same caution applies to money: amounts sent as floats can lose precision, so consider a decimal type when you adapt the generated code for financial values.

Generated code is a starting point

A sample shows what one response looked like, not every response the API can return. Treat the output as a first draft: compare it with the API's documentation or OpenAPI schema, tighten union types into enums where values are fixed, and add validation where data comes from outside your system. If the API publishes a JSON Schema or OpenAPI document, generating from that is more reliable than generating from a sample.

Step by step

  1. Paste a sample JSON response or load a file.
  2. Choose the target language.
  3. Enter a root type name such as Order or UserProfile.
  4. Review the generated types, especially optional and loosely typed fields.
  5. Copy the code or download it as a source file.

Privacy

Type generation happens in your browser. Real API responses, including customer data or tokens in the sample, are never uploaded.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages