Converting JSON to Go Structs
Go is a statically typed language, which means you need concrete struct types to unmarshal JSON data. Writing these structs by hand is tedious and error-prone, especially when dealing with deeply nested API responses that span dozens of fields. This tool automates the entire process: paste your JSON, choose a root name, and get production-ready Go code in milliseconds.
The generated structs follow Go conventions — PascalCase exported field names, properly typed slices, pointer types for nullable fields — and include json struct tags so encoding/json can serialize and deserialize your data without any additional configuration.
How JSON Types Map to Go
- •
string→string - •
42→int - •
3.14→float64 - •
true/false→bool
- •
{...}→ named struct - •
["a","b"]→[]string - •
[1, 2]→[]int - •
null→*interface{}
Understanding JSON Struct Tags in Go
Go struct tags are metadata annotations placed after a field's type declaration inside backticks. The json tag tells the encoding/json package how to map between struct fields and JSON keys. Without tags, Go uses the field name directly, which can cause mismatches when your JSON uses snake_case or camelCase naming conventions.
UserName string `json:"user_name"` UserName string `json:"user_name,omitempty"` Internal string `json:"-"` Best Practices for Go Struct Design
- 1
Use pointer types for optional fields. When a JSON field can be null or absent, use a pointer type like *string or *int. This lets you distinguish between a missing field (nil) and a zero-value field (empty string or 0).
- 2
Add omitempty for request payloads. When building structs for API requests, omitempty prevents zero-value fields from being serialized, keeping your payloads clean and reducing bandwidth.
- 3
Name nested structs meaningfully. While auto-generated names based on the JSON key work, renaming structs to reflect their domain meaning (e.g., Address instead of ShippingInfo) improves code readability.
- 4
Validate generated code. Auto-generated structs are a starting point. Review the types, check for fields that should use custom types (like time.Time for timestamps), and add validation logic as needed.
- 5
Use json.RawMessage for dynamic shapes. When a JSON field can hold different types depending on context, use json.RawMessage to defer parsing until you know the concrete type at runtime.
Frequently Asked Questions
How does JSON to Go struct conversion work?
The converter parses your JSON input and inspects the type of each value. For each JSON object it encounters, it generates a Go struct with PascalCase field names and json struct tags that map back to the original key names. Nested objects become separate named structs, and arrays are typed based on the elements they contain.
What Go types are generated from JSON values?
JSON strings map to Go string, booleans to bool, integers to int, floating-point numbers to float64, arrays to typed slices like []string or []int, nested objects to named structs, and null values to pointer types. When an array contains mixed types, the converter falls back to []interface.
What does the omitempty tag option do in Go?
The omitempty option in a json struct tag tells Go's encoding/json package to omit the field from JSON output when it holds a zero value — empty string, 0, false, nil pointer, or empty slice/map. This is useful for optional API fields where you do not want to send default values.
How are nested JSON objects handled?
Each nested JSON object is extracted into its own named Go struct. The field in the parent struct references the child struct by name. Struct names are derived by converting the JSON key to PascalCase. For example, a key called user_profile becomes a struct named UserProfile with its own typed fields and json tags.
Can I convert a JSON array into Go structs?
Yes. If your top-level JSON is an array of objects, the converter merges all objects in the array to capture every possible key, then generates a single struct that covers all fields. This is especially useful for API responses that return collections of resources with slightly varying shapes.