JSON to Go Struct
Generate Go struct definitions from JSON data. Create idiomatic Golang structs with json tags, proper naming conventions, and type inference.
Features
đš Idiomatic Go Code
- PascalCase struct and field names
- Proper json struct tags
- Correct Go type inference
- Nested struct generation
⥠Smart Type Detection
- int64 vs float64 detection
- Nested object handling
- Array type inference
- time.Time for dates (optional)
đ¯ Production Ready
- omitempty for optional fields
- Pointer types support
- Package declaration
- Ready for json.Unmarshal
Go Struct Guide
Go JSON Struct Tags
Go uses struct tags to control JSON serialization. The json tag specifies the JSON field name, and omitempty excludes zero-value fields from the output.
type User struct {
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email,omitempty"`
}
Tips for production-ready Go structs
Go's encoding/json package is strict about exported fields and types, so a few small edits to generated structs make decoding far more robust once real traffic arrives.
Tips and common pitfalls
- Only exported (capitalized) fields are decoded. The json struct tag maps them back to the original lowercase key.
- Numbers that can exceed 253, such as some IDs, are safer as
stringwith the,stringtag option or asjson.Number. - Use
time.Timefor RFC 3339 timestamps; it decodes them directly without extra code. - Call
decoder.DisallowUnknownFields()in tests to catch fields your struct does not know about yet.
More questions
What is the difference between a nil slice and an empty slice in JSON?
A nil slice encodes as null while an empty slice encodes as []. Initialize slices with make or []T{} if API consumers expect an empty array.
Can I decode into a map instead of a struct?
Yes, map[string]any decodes any object, but you lose type safety and every value needs a type assertion. Structs are faster and catch mistakes at compile time.