JSON to Go Struct Converter
Convert JSON data to Go struct definitions instantly. Generates typed structs with json tags and proper Go naming.
How to Use
- Paste your JSON data into the left panel.
- Optionally change the Struct Name for the top-level type (defaults to AutoGenerated).
- Toggle Inline structs to embed nested structs directly instead of creating separate named types.
- Toggle JSON tags on or off to control whether
`json:"..."`annotations are included. - Enable omitempty to add the omitempty option to all JSON tags.
- The Go struct output updates automatically as you type — copy it with the Copy button.
Why Convert JSON to Go Structs?
Go is a statically typed language, and working with JSON data requires defining struct types that match the shape of the incoming data. When consuming REST APIs, reading configuration files, or processing webhook payloads, you need Go structs that correctly represent every field and its type. Writing these structs by hand is tedious and error-prone, especially for deeply nested JSON with dozens of fields.
This tool automates the process — paste a sample JSON response and get production-ready Go struct definitions in milliseconds. The generated code includes proper PascalCase field names, accurate type inference (distinguishing between integers and floats), and JSON struct tags that preserve the original key names for correct marshaling and unmarshaling.
Understanding Go Struct Tags
Struct tags are metadata annotations attached to struct fields in Go. The json tag is used by the encoding/json package to control how fields are serialized and deserialized. For example:
type User struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Age int64 `json:"age,omitempty"`
} Without struct tags, Go would expect JSON keys to match the exact PascalCase field names (e.g., "FirstName"), which rarely matches real-world API responses. Tags bridge the gap between Go naming conventions and JSON conventions.
Go Type Mapping
The converter maps JSON types to Go types as follows:
- String →
string - Integer number →
int64(uses int64 for maximum range compatibility) - Floating-point number →
float64 - Boolean →
bool - Null →
interface{}(since null can represent any type) - Nested object → a separate struct (or inline struct)
- Array of T →
[]T - Empty array →
[]interface{} - Mixed array →
[]interface{}
Using int64 instead of int ensures your structs work correctly across 32-bit and 64-bit platforms and can handle large numeric IDs commonly found in APIs (Twitter IDs, Snowflake IDs, etc.).
Inline vs Separate Structs
By default, each nested JSON object produces a separate named struct definition. This is the Go best practice because named types are reusable, testable, and self-documenting. For example, a JSON object with an "address" field generates both a root struct and an Address struct.
The inline option embeds anonymous structs directly. This is useful for quick prototyping or when the nested structure is only used once. However, inline structs cannot be referenced elsewhere in your code, so separate structs are generally preferred for production use.
The omitempty Option
Adding omitempty to a JSON tag instructs Go's JSON encoder to skip the field when it holds a zero value: an empty string, 0, false, nil pointer, or empty slice/map. This produces cleaner JSON output when marshaling Go structs, especially for optional fields. Note that omitempty has no effect on unmarshaling (decoding) — missing JSON fields are always set to their zero values regardless.
Common Patterns and Best Practices
When working with JSON APIs in Go, consider these patterns:
- Use pointer types for optional fields — after generating your struct, change fields like
Age int64toAge *int64to distinguish between "missing" (nil) and "zero" (0). - Use json.RawMessage for dynamic fields — if a JSON field can contain different shapes, use
json.RawMessageto defer parsing. - Validate after unmarshaling — struct tags handle mapping, but you should validate required fields, ranges, and formats in your application logic.
- Use json.Decoder for streams — for large JSON files or HTTP response bodies, prefer
json.NewDecoder(reader).Decode(&v)overjson.Unmarshal.
Handling Edge Cases
Real-world JSON APIs contain patterns that require careful handling in Go. Arrays with mixed types (e.g., [1, "hello", true]) are mapped to []interface{} because Go slices must be homogeneously typed. Null values in JSON indicate that a field exists but has no value — the converter uses interface{} for these since the concrete type cannot be determined from null alone. In production code, you would typically change these to pointer types (*string, *int64) based on your knowledge of the API.
When an array contains objects, the converter analyzes all objects to merge their fields into a single struct type. If different objects in the array have different fields, the resulting struct includes all fields, with types determined by the union of observed types. This ensures that every possible object in the array can be deserialized into the struct without data loss.
Related Tools
Generate TypeScript types from JSON with the JSON to TypeScript Generator. Validate your JSON before converting with the JSON Validator. Format messy JSON with the JSON Formatter. Generate JSON Schema definitions with the JSON Schema Generator.
Frequently Asked Questions
- How does JSON to Go struct conversion work?
- The tool parses your JSON data and infers Go types for each field — strings become string, integers become int64, floats become float64, booleans become bool, and null values become interface{}. Nested objects are converted into separate or inline struct definitions.
- What are JSON struct tags in Go?
- Struct tags like `json:"field_name"` tell Go's encoding/json package how to map JSON keys to struct fields. Since Go uses PascalCase for exported fields but JSON typically uses camelCase or snake_case, struct tags preserve the original JSON key names during marshaling and unmarshaling.
- What does the omitempty option do?
- Adding omitempty to a JSON struct tag (e.g., `json:"name,omitempty"`) tells Go's JSON encoder to omit the field from the output if it has a zero value — empty string, 0, false, nil, or an empty slice/map.
- What is the difference between inline and separate structs?
- Separate structs (the default) create a named type definition for each nested object, making them reusable. Inline structs embed anonymous struct definitions directly within the parent, which is more compact but cannot be reused elsewhere in your code.
- How are arrays handled in the conversion?
- Arrays of a single type become typed slices (e.g., []string). Arrays of objects generate a separate struct for the item type. Empty arrays and arrays with mixed types become []interface{} since Go requires a single element type.
- Why are field names capitalized in Go structs?
- In Go, only fields that start with an uppercase letter are exported (visible outside the package). Since struct fields used with encoding/json must be exported to be marshaled/unmarshaled, the tool capitalizes all field names and uses JSON tags to map them to the original keys.
- Is my JSON data sent to a server?
- No. All conversion happens entirely in your browser using JavaScript. No data is transmitted to any server — your JSON stays private.
- How are snake_case and camelCase keys handled?
- The tool converts both snake_case (e.g., user_name) and camelCase (e.g., userName) keys to PascalCase for Go field names (UserName). The original key is preserved in the JSON struct tag for correct serialization.
Use this tool from AI agents.
The CodeTidy MCP Server lets Claude, Cursor, and other AI agents
use this tool and 46 others directly. One command: npx @codetidy/mcp