JSON to Code Generator
JSONGenerate TypeScript, Java, Go and C# entity types from JSON. Infers nested objects and arrays.
Remote URLs are not fetched; paste your JSON directly.
On this page
What is a JSON to code generator?#
A JSON to code generator reads a sample JSON document, works out the shape of the data — which fields are strings, which are numbers, which are nested objects, which are arrays of what — and writes out the entity types a real program would use to hold that data. Instead of hand-coding a TypeScript interface, a Java class, a Go struct, or a C# class to match an API response, you paste one example response and get the types ready to drop into your project.
Under the hood it walks the parsed value and infers a schema. Nested objects become their own named type; an array’s element type is the merge of every element’s shape (so a list of objects that do not all have the same keys still produces one type with the union of fields). Field names that are not valid identifiers — a hyphenated key like first-name, or one starting with a digit — are sanitized into a legal identifier and the original key is preserved as a serialization annotation, so round-tripping still works.
The output is plain-text source code with no runtime dependency on this page. Copy it, paste it into your codebase, adjust the type names, and you have a starting point — not a finished model, but the boring skeleton already written for you.
How to use it#
- Pick the target with the Language dropdown: TypeScript, Java, Go, or C#.
- Set the Root name (default
Root). This becomes the name of the outermost type — for exampleUser,Order,Response. - Paste your JSON into the left pane. The generated code appears live on the right; the status line reports errors (invalid JSON, empty input).
- Click Copy to take the code to the clipboard.
- Sample loads a nested object with a sub-object and an array; Clear resets both panes.
Key features#
- Four target languages. TypeScript interfaces, Java classes, Go structs, and C# classes — one input, four outputs, switched with a dropdown.
- Schema inference, not literal typing. Nested objects become named types; arrays of objects produce a named element type rather than an opaque
anylist. - Heterogeneous-array merging. When array elements have differing fields, the inferred type is the union — useful for messy real-world responses where not every item has every key.
- Identifier-safe, key-faithful. Keys that are not valid identifiers are sanitized, and the original key is kept as a serialization annotation (
@JsonProperty, a Gojson:tag,JsonPropertyName), so the original JSON still deserializes correctly. - Live and local. Generation runs in your browser as you type or switch options; the data never leaves the page.
Worked example#
Load Sample with Language set to TypeScript and Root name Root, and the input is:
{
"id": 1,
"name": "Ada",
"active": true,
"address": { "city": "London", "zip": "NW1" },
"tags": ["math", "logic"]
}
The generator emits two interfaces — the root, and a named Address type for the nested object:
export interface Root {
id: number;
name: string;
active: boolean;
address: Address;
tags: Array<string>;
}
export interface Address {
city: string;
zip: string;
}
Switch the language to Go and the same input becomes structs with json: tags that preserve the original keys:
package model
type Root struct {
Id float64 `json:"id"`
Name string `json:"name"`
Active bool `json:"active"`
Address Address `json:"address"`
Tags []string `json:"tags"`
}
type Address struct {
City string `json:"city"`
Zip string `json:"zip"`
}
Note how id, a JSON number, lands as number in TypeScript and float64 in Go — the generator does not guess integer versus floating-point, because JSON itself does not distinguish them.
FAQ#
Why is my integer field typed as float64 / Double / double?#
Because JSON has a single number type and no way to tell 5 from 5.0. The generator maps every JSON number to the target language’s general numeric type — number in TypeScript, Double in Java, float64 in Go, double in C# — so the type is always wide enough to hold whatever the value turns out to be. If you know a field is always an integer, narrow it to int / Long / int64 by hand afterwards.
What happens when my array’s objects do not all have the same fields?#
Their shapes are merged. An array like [{ "a": 1 }, { "a": 2, "b": "x" }] produces one element type with both a and b, so a single type covers every item. If two elements disagree on a field’s type in an incompatible way (say a number here, a string there), that field degrades to any / Object rather than picking one side and being wrong about the other.
A field in my JSON has a hyphen, like first-name. Will that break?#
No. The generator turns first-name into a legal identifier (firstName) for the field name and emits a serialization annotation that records the original key — @JsonProperty("first-name") in Java, a json:"first-name" tag in Go, [JsonPropertyName("first-name")] in C#, and a quoted "first-name" key in TypeScript. The generated code therefore deserialises the original JSON unchanged.
What does it do with a null value?#
A field whose value is null is treated as unknown type. In TypeScript it becomes an optional any (field?: any;); in Java and C# it becomes Object / object; in Go, interface{}. That is a deliberate signal — null tells you nothing about what the field holds when it is present, so the generator refuses to guess and leaves it for you to fill in.