JSON to Code (TypeScript, Go, Python, Java, C#, Rust, Kotlin, Swift)
Paste a sample API response and get matching types in eight languages, with nested types, optional and nullable fields, and annotations that keep the original JSON names.
S'executa al teu navegador. Els fitxers mai no surten del dispositiu. Mira com comprovar-ho
…
Com es fa
- 1Paste sample JSON
Use a real response with as many fields as possible.
- 2Choose the language
Pick one of eight.
- 3Name the root type
For example Order or User.
- 4Review
Check optional and nullable fields.
- 5Refine
Tighten loose types and add enums.
- 6Copy or download
Add the file to your project.
Sobre aquesta eina
Writing types by hand for a JSON API is slow and error-prone: a field that is sometimes missing, a number that is sometimes a decimal, a nested object three levels down. This generator infers the types from a sample. Objects become named types, nested objects get their own types named after their keys, arrays become typed lists, and when an array contains several objects their shapes are merged: a field missing from some items becomes optional and a field that is sometimes null becomes nullable. Whole numbers and decimals are told apart, and an array seen with both becomes a decimal type. The output follows each language's conventions: TypeScript interfaces, Go structs with json tags and omitempty, Python dataclasses with snake_case names, Java records with Jackson annotations, C# records with System.Text.Json attributes, Rust structs with serde renames, Kotlin data classes with kotlinx.serialization and Swift Codable structs with CodingKeys when names differ.
How types are inferred
- Strings, booleans, whole numbers and decimals map to each language's natural types. - Nested objects become separate named types; names come from the key in PascalCase, and array item types use the singular form (items becomes Item). - Arrays of objects are merged into one type covering every item. - Optional: a field missing from at least one item. - Nullable: a field that is null in at least one item. - Unknown: an empty array or a value that is only ever null gets a loose type such as unknown, Any or interface'{}'.
Naming
Field names are converted to each language's style (camelCase, snake_case or PascalCase) and the original JSON key is kept with an annotation or tag whenever it differs, so serialisation still matches the API.
Choosing a good sample
Use a response that shows as many fields as possible, including optional ones, and include an array with several items so variations are seen. Two or three real responses combined into one array give the most accurate types.
After generating
Rename types to suit your domain, replace loose types with precise ones, and add enums for fields with a fixed set of values. If the API publishes an OpenAPI or JSON Schema document, prefer generating from that, because a sample cannot show every possibility.
Limits
Types reflect one sample only; values the sample does not show are unknown. Date strings stay strings. Very large integers lose precision in JavaScript; use strings for such IDs. Review generated code before committing it.
Exemple resolt
An order with an id, a customer object and an items array where only some items have gift: true becomes, in TypeScript, an Order interface with customer: Customer and items: Item[], where Item has sku, qty, price and an optional gift?: boolean, and coupon is typed as null.
En resum
- Categoria: Desenvolupament i dades
- S'executa al navegador, sense registre, sense pujar res.
- La informació que introdueixes es queda al dispositiu.
- Disponible en anglès.
- Contingut revisat per última vegada:
- Font oficial: Language documentation: TypeScript interfaces, Go encoding/json, Python dataclasses, serde, Jackson, System.Text.Json
Preguntes freqüents
Which languages are supported?
TypeScript, Go, Python, Java, C#, Rust, Kotlin and Swift.
How are optional fields detected?
By merging all items of an array: a field missing from any item becomes optional.
Are JSON key names preserved?
Yes, with tags or annotations whenever the field name in code differs.
Can it read JSON Schema?
No. It infers from a sample; use a schema generator when a schema exists.
Is my data uploaded?
No. Types are generated in your browser.
Why is a field typed loosely?
It was null or an empty array in the sample, so its real type is unknown.
