How it works
Paste a JSON sample and get Go structs ready for encoding/json:
- Every object becomes a
type Name struct, named after its property; the root isRootunless you rename it. - Field names are exported PascalCase with Go initialisms (
ID,URL,HTTP,JSON,UUID…), and every field keeps the original key in ajson:"…"tag. - Arrays of objects are merged into one struct, so fields that appear in only some items are still captured.
- Numbers are split into
int64andfloat64, booleans becomebool, empty objects becomemap[string]anyand empty arrays[]any. - Optional and nullable fields become pointers (see the FAQ); optional fields also get
,omitempty. - Field names, types and tags are aligned in columns, as
gofmtwould format them.
Example
{
"users": [
{ "id": 1, "nick": "a", "avatar": null },
{ "id": 2, "nick": null, "avatar": { "url": "x" } }
],
"big": 9007199254740993
}
becomes
type Root struct {
Users []User `json:"users"`
Big int64 `json:"big"`
}
type User struct {
ID int64 `json:"id"`
Nick *string `json:"nick"`
Avatar *Avatar `json:"avatar"`
}
type Avatar struct {
URL string `json:"url"`
}
A root array produces a named slice type, e.g. type Root []int64.
Options
- Root name — name of the top-level type.
- Package — prepend a
packageclause so the output is a complete.gofile. - omitempty on all — add
,omitemptyto every tag, not just optional fields. Note that it also drops zero values such as0,falseand""when marshaling.
Common mistakes
- Generating from a single object. Every key it contains becomes a required, non-pointer field. Paste an array of several real responses so optional fields get pointers and
,omitempty. - Numbers that happen to be whole. A price of
25in the sample becomesint64. If a later response sends25.5,json.Unmarshalfails with a “cannot unmarshal number … into Go struct field” error. Include a decimal value in the sample, or change the type tofloat64by hand. - Invalid sample JSON. The error panel shows the line and column and offers Fix with JSON Repair for comments, trailing commas and other near-JSON.
Decoding with the generated structs
var root Root
if err := json.Unmarshal(data, &root); err != nil {
return fmt.Errorf("decode: %w", err)
}
for _, u := range root.Users {
if u.Nick != nil { // nil when the value was null
fmt.Println(*u.Nick)
}
}
To reject payloads with unexpected keys, use a decoder with dec.DisallowUnknownFields().
Refining the output
- Timestamps arrive as strings; change the field to
time.Timeif they are RFC 3339. - Swap
int64forintoruint32where the range is known, andfloat64for a decimal type for money. - Rename struct types if a name reads awkwardly. Element types are named with the singular of the array key (
addresses→Address,categories→Category), which is a heuristic, not a dictionary.
Working in TypeScript or Python too? Generate matching types with JSON to TypeScript or JSON to Pydantic, or describe the payload language-neutrally with the JSON Schema Generator.
Frequently asked questions
When does a field become a pointer?
Two cases. A key that is missing from some objects in an array becomes a pointer with `,omitempty` (for example `*bool`), so you can tell "absent" from the zero value. A field whose sample values include null and one real type becomes a pointer too (`*string`, `*Avatar`). Slices and maps are never wrapped, because they are already nil-able.
Why is a field typed as `any`?
Either every sample value was null, or the field mixes incompatible types (for example a number in one object and a string in another). Replace `any` with the real type once you know it.
How are numbers typed?
Whole numbers become `int64`. If any sample value for the field has a decimal part, it becomes `float64`. Check fields like prices that happened to be whole in your sample.
Are Go naming conventions followed?
Yes. Keys in snake_case, camelCase or kebab-case are converted to exported PascalCase names, and common initialisms are upper-cased as the Go style guide recommends — `user_id` → `UserID`, `avatarUrl` → `AvatarURL`. The original key is always kept in the json tag.
Is my JSON uploaded?
No. The structs are generated entirely in your browser — nothing is uploaded.
Last updated