JSON to Go Struct Generator
Paste JSON, get Go structs — nested objects, arrays, json tags, omitempty — copy or download as .go
Options
428 characters
4 structs generated · 605 characters
You’re integrating a REST API in Go and need a struct that matches the JSON response. The response has 30 fields, nested objects, arrays of mixed types, and fields that are sometimes null. Writing the struct by hand means 30 lines of FieldName type \json:“field_name”“ tags, and one typo in the JSON tag means silent data loss — the field just stays at its zero value with no error.
Why This Generator (Not json-to-go.dev)
json-to-go.dev is the original tool by Matt Holt — and it works. This tool adds omitempty toggle, pointer types for nullable fields, and inline vs separate struct declarations — options that matter for production Go code. Everything runs in your browser; no data is sent anywhere.
What Is a JSON to Go Struct Generator?
When building Go applications that consume REST APIs, parse configuration files, or process any JSON data, you need Go struct types that match the JSON structure precisely. Writing these structs by hand is tedious — especially for deeply nested JSON with many fields.
A JSON to Go struct generator reads your JSON and automatically produces correct Go struct declarations, handling:
- Nested objects (each becomes its own named struct)
- Arrays of objects (all objects merged into a single struct with consistent field types)
- Primitive values (
string,int,float64,bool) - Mixed arrays falling back to
[]interface{} nullvalues mapped tointerface{}- Automatic
json:""struct tags for marshaling/unmarshaling
This tool runs entirely in your browser — your JSON data never leaves your device.
How to Use This Tool
- Paste your JSON in the JSON Input textarea (or click Load sample to try an example)
- Configure options: root struct name, export fields, JSON tags, omitempty, inline structs, pointer types
- The Go struct output updates instantly in the Go Output panel
- Click Copy to copy to clipboard, or Download .go to save as a
.gofile
Understanding the Generated Structs
Field Naming Convention
JSON keys are converted to idiomatic Go field names using the following rules:
camelCase→CamelCase(split on case boundaries)snake_case→SnakeCase(split on underscores)kebab-case→KebabCase(split on hyphens)ALL_CAPS→AllCaps- Keys starting with digits get an
Fprefix:123key→F123key - Empty or entirely non-alphanumeric keys →
Field
Struct Naming
Nested struct names are derived from the parent struct name plus the field name in PascalCase:
- Root object → name you specify (default:
Root) - Nested field
address→RootAddress - Nested field
socialinsideprofile→RootProfileSocial - Array element structs → parent name +
Item: e.g.,RootPostsItem
Type Mapping
| JSON type | Go type |
|---|---|
string | string |
| integer number | int |
| floating-point number | float64 |
boolean | bool |
null | interface{} |
object {} | named struct |
| array of objects | []StructName |
| empty array | []interface{} |
| mixed-type array | []interface{} |
Options Explained
Root Name
The name given to the top-level struct. Defaults to Root. If your JSON represents a User record, set the root name to User for cleaner, idiomatic Go code.
Export Fields
When enabled (default), all field names start with an uppercase letter, making them exported (publicly accessible). Disable for unexported fields — uncommon but occasionally useful in internal packages.
JSON Tags
When enabled (default), each field gets a json:"key" struct tag. These tags are required by encoding/json for correct field mapping when the JSON key differs from the Go field name (e.g., snake_case JSON keys with PascalCase Go fields).
Example with JSON tags:
type User struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
IsActive bool `json:"is_active"`
}
omitempty
Appends ,omitempty to every JSON tag. When marshaling to JSON with encoding/json, fields with zero values ("", 0, false, nil) are omitted from the output:
type User struct {
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
}
Useful for API request payloads where omitting zero-value fields is desirable.
Inline Structs
By default, nested structs are declared as separate top-level types. With inline structs enabled, nested types are declared as anonymous structs within the parent:
Separate (default):
type Root struct {
Profile RootProfile `json:"profile"`
}
type RootProfile struct {
Bio string `json:"bio"`
}
Inline:
type Root struct {
Profile struct {
Bio string `json:"bio"`
} `json:"profile"`
}
Separate declarations are generally preferred — they are reusable, testable, and easier to reference by name.
Pointer Types
When enabled, optional fields (present in some but not all array elements) are generated as pointer types (*string, *int, *bool) instead of value types. This distinguishes between a missing field (nil) and a zero-value field ("", 0, false):
type RootPostsItem struct {
ID int `json:"id"`
Title string `json:"title"`
Published *bool `json:"published"` // optional: nil if absent
}
Handling Arrays of Objects
When a JSON array contains objects, the generator merges all objects in the array into a single struct. Properties that exist in only some of the elements are still included — the Go type system doesn’t have per-field optionality, so all fields appear in the struct:
Input JSON:
{
"posts": [
{ "id": 1, "title": "Hello World", "published": true },
{ "id": 2, "title": "Go Tips" }
]
}
Generated output:
type Root struct {
Posts []RootPostsItem `json:"posts"`
}
type RootPostsItem struct {
ID int `json:"id"`
Title string `json:"title"`
Published bool `json:"published"`
}
With pointer types enabled, Published becomes *bool since it’s absent from some objects.
Handling null Values
JSON null maps to interface{}:
type Root struct {
Name string `json:"name"`
DeletedAt interface{} `json:"deleted_at"`
}
In practice, you may want to refine this to *string or a custom nullable type. The tool generates types based on the actual data provided.
Common Use Cases
Consuming REST APIs
Paste the raw JSON response from a REST API endpoint to instantly generate Go structs for unmarshaling:
resp, err := http.Get("https://api.example.com/users/1")
var user User
json.NewDecoder(resp.Body).Decode(&user)
Configuration File Parsing
If your Go application reads a JSON configuration file, generate structs from a sample config to get type-safe access to all configuration fields.
Third-Party API Integration
Most REST APIs provide sample responses in their documentation. Paste the sample and get the struct definitions needed for your integration layer.
gRPC/Protobuf Migration
When migrating from JSON-based APIs to gRPC, use the generated structs as a reference for defining your .proto message types.
After Generating: Refining Your Structs
The auto-generated structs are a solid starting point. Consider these refinements:
-
Add
time.Timefor date/time fields:CreatedAt time.Time `json:"created_at"` -
Use custom types for string enums:
type Status string const ( StatusActive Status = "active" StatusInactive Status = "inactive" ) -
Add validation struct tags (for libraries like
go-playground/validator):Email string `json:"email" validate:"required,email"` -
Deduplicate identical structs — if two generated structs are structurally identical, consolidate them into one.
-
Add methods — attach
Validate(),String(), or other methods to named types.
FAQ
Does this tool send my JSON to a server?
No. All processing happens entirely in your browser using JavaScript. Your JSON data never leaves your device.
What Go package does the download include?
Downloaded .go files include package main at the top. Change this to the appropriate package name for your project (package models, package types, etc.).
How are empty objects {} handled?
An empty JSON object generates a struct with no fields:
type Root struct{}
What about duplicate struct names?
If two different JSON paths produce the same struct name, the generator appends a numeric suffix (RootItem, RootItem2) to avoid conflicts.
Can I use the generated structs with encoding/json directly?
Yes. The generated struct tags (json:"key") are designed for use with Go’s standard encoding/json package. Both json.Marshal and json.Unmarshal will work correctly with the default options.
What if my JSON has keys that are Go reserved words?
Go reserved words like type, func, map, range — when used as JSON keys — are capitalized to Type, Func, Map, Range. These are valid Go field names and won’t conflict with the language keywords.
Does this support Go generics?
The generator produces standard struct types. Generic struct generation (e.g., type Response[T any] struct { Data T }) is not supported — this would require knowledge of multiple related JSON shapes.