About the JSON to Go Struct Generator
Go decodes JSON into structs whose exported field names are matched to keys through struct tags. Writing those tags by hand for a large payload is where typos creep in. This generator derives them: each key becomes an exported PascalCase field with a json:"key" tag holding the original name, and each nested object becomes its own named struct.
Types follow the encoding/json defaults: string, bool, float64 for decimals, int64 for whole numbers, slices for arrays and interface{} for nulls. Fields are emitted with tab indentation so gofmt leaves them unchanged.
The generator does not add omitempty or pointer types, because whether a field may be absent is not visible in a single sample. Add them where the API documents optional fields. Time values arrive as strings; change the type to time.Time if the format is RFC 3339.
How to use
- Paste a JSON sample on the left and name the root struct.
- Copy the structs into a Go file in your package.
- Decode with
json.Unmarshal(data, &root).
Common questions
- Why int64 rather than int?
- JSON numbers can exceed 32 bits, and int64 is safe on every platform. Change to int where you know the range.
- How do I make a field optional?
- Use a pointer type and add
,omitemptyto the tag, for example*string \`json:"nickname,omitempty"\`. - Are nested structs inlined or separate?
- Separate named structs, which keeps them reusable and readable. Inline them by hand if you prefer.