JSON to Go Struct Generator

Paste any JSON and instantly get idiomatic Go struct definitions with correct types and json struct tags. Free, no account needed, runs entirely in your browser.

Open JSON to Go Generator →

What are Go Structs?

In Go, a struct is a composite data type that groups named fields together. Structs are how Go represents structured data - they are the equivalent of classes in object-oriented languages, but simpler and without inheritance. When you work with JSON in Go, you define structs that mirror the shape of your JSON data, then use the standard library's encoding/json package to convert between them.

A basic Go struct for a JSON user object looks like this:

type User struct {
    ID       int    `json:"id"`
    Name     string `json:"name"`
    Email    string `json:"email"`
    IsActive bool   `json:"is_active"`
}

Go's encoding/json package uses these struct field names and tags to marshal Go values to JSON and unmarshal JSON back to Go values. Writing these structs by hand for large API responses is slow and error-prone. This generator automates the process entirely.

Go structs are used across the entire Go ecosystem: HTTP handlers with the net/http package, API clients, CLI tools, cloud functions, microservices, and database models all use structs as the fundamental unit of data representation.

JSON Tags in Go: The Complete Guide

Struct tags in Go are string literals placed after a field declaration that provide metadata to reflection-based libraries. The encoding/json package reads the json tag on each field to determine how to handle it during marshaling and unmarshaling.

Tag Effect Example
json:"name"Use "name" as the JSON keyjson:"user_id"
json:"-"Always omit this fieldjson:"-"
json:",omitempty"Skip if zero valuejson:"score,omitempty"
json:",string"Encode number as JSON stringjson:"id,string"

Field names in Go structs must start with an uppercase letter to be exported (accessible outside the package) and therefore visible to encoding/json. Unexported (lowercase) fields are silently ignored during marshaling and unmarshaling. Our generator automatically capitalizes field names and adds the correct lowercase json tag to match the original JSON key.

How to Use the JSON to Go Generator

Generating Go structs from JSON takes only a few seconds with this tool. Follow these steps:

  1. Obtain a sample JSON payload - from a REST API response, a file, or write it by hand
  2. Click "Open JSON to Go Generator" above to open the tool
  3. Paste your JSON into the input area
  4. The generator immediately outputs the corresponding Go struct definitions
  5. Copy the generated code into your Go source file
  6. Import encoding/json and use json.Unmarshal or json.NewDecoder to parse real data

The generator follows Go naming conventions: JSON keys in snake_case or camelCase are converted to PascalCase Go identifiers. Common abbreviations like id, url, and http are automatically uppercased to ID, URL, and HTTP per the official Go style guide.

Handling Nested JSON in Go

Real-world JSON is rarely flat. API responses often contain deeply nested objects and arrays of objects. Go handles this elegantly through nested struct definitions. Each nested JSON object becomes its own Go struct, and the parent struct contains a field of that struct type.

For example, a JSON response containing an order with a nested customer and an array of line items:

type Order struct {
    OrderID  int       `json:"order_id"`
    Customer Customer  `json:"customer"`
    Items    []Item    `json:"items"`
    Total    float64   `json:"total"`
}

type Customer struct {
    ID    int    `json:"id"`
    Name  string `json:"name"`
    Email string `json:"email"`
}

type Item struct {
    SKU      string  `json:"sku"`
    Quantity int     `json:"quantity"`
    Price    float64 `json:"price"`
}

Arrays of objects become Go slices. A JSON array of objects at the top level generates a slice type such as []Item. When unmarshaling, pass a pointer to a slice: var items []Item; json.Unmarshal(data, &items).

Frequently Asked Questions

What are Go structs and how do they relate to JSON?

Go structs are composite data types that group named fields. They are Go's primary tool for representing structured data. When you work with JSON in Go, you define a struct that matches the JSON schema, then use encoding/json to unmarshal JSON bytes into the struct or marshal the struct back to JSON. The struct field names and json tags control the mapping between Go field names and JSON key names.

What are json struct tags in Go?

Struct tags are metadata strings attached to struct fields. The json tag tells encoding/json how to handle each field. json:"user_id" maps the field to the "user_id" JSON key. json:",omitempty" skips the field during marshaling if it holds the zero value for its type. json:"-" completely excludes a field from JSON processing. Without a tag, encoding/json uses the exact Go field name as the JSON key.

How does the generator handle null values and optional fields in Go?

Go does not have a built-in concept of nullable primitive types. To represent optional or nullable fields, the generator uses pointer types such as *string or *int. A nil pointer corresponds to a JSON null or a missing field. The omitempty tag option is added to fields that may be absent. For fields that should accept null explicitly, the generator uses pointer types so the distinction between "missing" and "present but null" can be made at runtime.

How are nested JSON objects handled when generating Go structs?

Each nested JSON object generates a separate named struct. The parent struct contains a field of the nested struct type. All structs are output together in a single block. Arrays of objects generate slice fields such as []ItemStruct. The generator picks struct names from the JSON key names, converting them to PascalCase. You can rename the structs freely after generation - the json tags on the fields preserve the correct JSON mapping regardless of the Go struct name.

Ready to generate your Go structs?

Free, instant, 100% private. No account needed.

Open JSON to Go Generator →

Also useful: JWT Decoder | JSON Validator | JSON Formatter | JSON to Java | JSON to C# | JSON to Kotlin