🔧 JSON Tool Pro

Format · Validate · Diff · Java POJO · TypeScript · CSV · Escape · JSONPath

← Back to Blog

JSON to Go Struct — Complete Guide

Published: July 22, 2026

When building Go applications that consume JSON APIs, one of the first tasks is defining struct types that match the JSON structure. Manually typing out struct definitions for complex JSON can be tedious and error-prone. This guide covers both manual conversion and using automated tools.

Quick — Use Our Free Tool

If you just want to generate Go structs from JSON instantly, use our JSON Code Generator. Paste your JSON, select "Go", and get your struct definition in seconds.

Manual Conversion Rules

Understanding how JSON maps to Go types helps you write better structs:

JSON TypeGo TypeExample
stringstring"Alice"
numberfloat64 / int25
booleanbooltrue
nullinterface / pointernil
objectstructtype X struct
array[]Type[]string

Example: API Response to Go Struct

Input JSON:

{
  "id": 42,
  "name": "Alice Smith",
  "email": "alice@example.com",
  "age": 28,
  "isActive": true,
  "address": {
    "city": "Beijing",
    "zip": "100001"
  },
  "tags": ["developer", "golang"],
  "projects": [
    {"name": "Project A", "stars": 120},
    {"name": "Project B", "stars": 85}
  ]
}

Generated Go Struct:

type User struct {
    ID       int         `json:"id"`
    Name     string      `json:"name"`
    Email    string      `json:"email"`
    Age      int         `json:"age"`
    IsActive bool        `json:"isActive"`
    Address  Address     `json:"address"`
    Tags     []string    `json:"tags"`
    Projects []UserProject `json:"projects"`
}

type Address struct {
    City string `json:"city"`
    Zip  string `json:"zip"`
}

type UserProject struct {
    Name  string `json:"name"`
    Stars int    `json:"stars"`
}

JSON Tags in Go

Go struct tags control how JSON is encoded and decoded. Key tag options:

  • json:"field_name" — maps to a different JSON key name
  • json:"-" — ignores the field during encoding/decoding
  • json:"field_name,omitempty" — omits the field if it's empty
  • json:"field_name,string" — forces string representation for numbers

Best Practices

  1. Use explicit field names — always include JSON tags to handle different naming conventions (snake_case, camelCase)
  2. Handle optional fields — use pointer types (*string) or omitempty for fields that may not be present
  3. Use json.RawMessage for dynamic JSON fields where the structure isn't known in advance
  4. Define nested types — break complex JSON into separate struct types for readability
  5. Use a tool — for large JSON payloads, always use a JSON to Go generator to avoid manual errors

Handling Dynamic JSON

Sometimes JSON has dynamic keys or unknown structures. Use map[string]interface or json.RawMessage:

// Dynamic keys
type Response struct {
    Data map[string]interface{} `json:"data"`
}

// Raw JSON passthrough
type Response struct {
    Raw json.RawMessage `json:"-"`
}

Try It Yourself

Head over to our JSON Code Generator, paste your JSON, select Go, and generate your struct in one click. It's free, runs entirely in your browser, and your data never leaves your device.