JSON to Go Struct
Generate Go struct definitions from a JSON sample.
Overview
The JSON-to-Go converter produces Go struct declarations from a sample JSON document. Field names are exported (capitalised), types are inferred from the JSON values, and json: struct tags preserve the original key spellings.
It's the standard scaffolding move when consuming a JSON API in Go: paste a real response, get back a struct you can pass straight to json.Unmarshal. Go developers use a json to go struct generator to skip the keystrokes of typing out field declarations for verbose API payloads.
How it works
The generator walks the JSON tree and emits a struct for the root and every nested object. Field names are derived by upper-casing the first letter and PascalCasing the rest; the original key goes into a json:"..." tag so unmarshalling matches the source.
Type inference maps JSON to Go: strings to string, integers to int or int64 based on magnitude, decimals to float64, booleans to bool, arrays to []T with T derived from the elements. Heterogeneous arrays use []interface{}. Null fields are flagged with omitempty on the tag so they don't ship as null when the field is the zero value.
Examples
Sample:
{ "id": 1, "name": "Alice", "tags": ["admin","ops"], "address": { "city": "Berlin" } }
type Root struct {
Id int `json:"id"`
Name string `json:"name"`
Tags []string `json:"tags"`
Address Address `json:"address"`
}
type Address struct {
City string `json:"city"`
}
// Pointer types for optional fields:
type User struct {
Id int `json:"id"`
Email *string `json:"email,omitempty"`
}
FAQ
Why are some fields pointers?
Pointer types let you distinguish "field absent" from "field present with zero value". The generator uses *T when a sample value is null or when a key is missing in some objects of an array.
How are JSON numbers larger than int32 handled?
Integers up to int32 range use int; anything bigger uses int64. Decimals always go to float64 — Go has no decimal type in the standard library, and math/big is rarely the right default.
What about time.Time for date strings?
ISO 8601 strings stay typed as string by default because Go's JSON package requires explicit time parsing. Toggle the option to emit time.Time if every date in your data is RFC 3339.