JSON to Go Struct
Generate Go structs with JSON tags from sample JSON.
Last updated: August 2026
Was this tool helpful?
From sample JSON to typed Go structs
Go has no dynamic maps-of-anything that are pleasant to work with, so decoding JSON idiomatically means defining a struct with field types and json: tags. Writing those by hand for a large payload is tedious and error prone. A generator reads one representative sample and infers the struct layout, including nested types for objects and slices for arrays.
Example
Input:
{ "user_id": 7, "name": "Ada", "tags": ["a"] }Generated Go:
type AutoGenerated struct {
UserID int `json:"user_id"`
Name string `json:"name"`
Tags []string `json:"tags"`
}Note how user_id becomes the exported field UserID while the tag keeps the original snake_case key. Exported (capitalized) fields are required or encoding/json cannot populate them.
Pitfalls the sample cannot show
Inference only knows what is in the sample. A field that is null or absent in your example gives the generator nothing to work with, so it may guess interface or omit the field. Numbers are the other trap: JSON has one number type, so 1 infers as int even when the real data later contains 1.5. If a field can be fractional, widen it to float64 by hand.
FAQ
Should I add omitempty? Add it to fields that are optional on output so Go skips zero values during marshaling. The generator cannot know your intent, so this is a manual pass.
How do I handle a field that is sometimes a string and sometimes a number? Mixed-type JSON forces json.RawMessage or a custom UnmarshalJSON. Generators flag these as interface at best.
What about Python or Rust? See JSON to Python and JSON to Rust for the same idea in other languages.