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.

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.

JSON to Go Struct Conversion

Go structs with json tags are the idiomatic way to work with JSON in Go. The encoding/json package handles marshaling and unmarshaling automatically.

JSON Input → Go Struct

// JSON
{"user_id": 1, "name": "Alice", "is_active": true, "score": 98.5}

// Go Struct
package main

import "encoding/json"

type User struct {
    UserID   int     `json:"user_id"`
    Name     string  `json:"name"`
    IsActive bool    `json:"is_active"`
    Score    float64 `json:"score"`
}

func main() {
    var user User
    err := json.Unmarshal([]byte(jsonString), &user)
    if err != nil { panic(err) }
}

Go JSON Type Mappings

JSONGo TypeNotes
stringstring
integerint / int64
floatfloat64
booleanbool
null*Type (pointer)nil when null
array[]Type
objectstruct / map[string]interface{}

Go JSON Tips

// Omit empty fields
type User struct {
    Name  string `json:"name,omitempty"`
    Email string `json:"email,omitempty"`
}

// Ignore a field
type User struct {
    Password string `json:"-"`
}

// Marshal to string
bytes, _ := json.Marshal(user)
jsonStr := string(bytes)

// Pretty print
bytes, _ := json.MarshalIndent(user, "", "  ")

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

How to Convert JSON to Go Struct — Step by Step

  1. Paste your JSON into the converter above
  2. Click Convert — the tool maps JSON types to Go types with json tags
  3. Copy the struct definitions into your Go file
  4. Import encoding/json
  5. Use json.Unmarshal or json.NewDecoder to parse JSON
// Generated struct from JSON
type User struct {
    Name    string  `json:"name"`
    Age     int     `json:"age"`
    Email   string  `json:"email"`
    Active  bool    `json:"active"`
    Score   float64 `json:"score"`
    Tags    []string `json:"tags"`
    Address Address `json:"address"`
}

type Address struct {
    City    string `json:"city"`
    Country string `json:"country"`
}

// Unmarshal JSON
var user User
err := json.Unmarshal([]byte(jsonStr), &user)
if err != nil {
    log.Fatal(err)
}
fmt.Println(user.Name) // "Alice"

// Or with a decoder (for streams)
decoder := json.NewDecoder(reader)
err = decoder.Decode(&user)

JSON to Go Type Mapping Reference

JSON Type Go Type Notes
stringstringDirect mapping
number (integer)int / int64Use int64 for large numbers
number (float)float64Standard Go float
booleanboolDirect mapping
null*Type (pointer)nil pointer = null
array[]TypeSlice of element type
objectstruct / map[string]interface{}Struct for typed, map for dynamic

Working with Optional Fields in Go

Go does not have nullable primitives. Use pointer types for optional fields, and omitempty to skip zero-value fields during marshaling:

// Pointer fields for optional values
type Config struct {
    Host    string  `json:"host"`
    Port    *int    `json:"port,omitempty"`    // nil if absent
    Timeout *int    `json:"timeout,omitempty"` // nil if absent
}

// Custom UnmarshalJSON for complex cases
func (c *Config) UnmarshalJSON(data []byte) error {
    type Alias Config
    aux := &struct{ *Alias }{Alias: (*Alias)(c)}
    if err := json.Unmarshal(data, aux); err != nil {
        return err
    }
    if c.Port == nil {
        defaultPort := 8080
        c.Port = &defaultPort
    }
    return nil
}