← All JSON guides

JSON guide · 7 min read

How to Generate Go Structs from JSON

Generate Go structs with JSON tags and review pointers, numbers, optional fields and nested types.

Published 2026-08-05Reviewed 2026-08-05

Struct fields and JSON tags

Exported Go field names map to JSON property names through struct tags. Tags preserve names such as snake_case while keeping Go identifiers idiomatic.

type User struct {
    UserID string `json:"user_id"`
}

Choose optionality deliberately

A zero value does not distinguish a missing field from an explicitly supplied zero. Use pointers, nullable wrappers or custom types when that distinction matters.

Be careful with numbers

JSON has one number syntax, while Go has many integer and floating types. Large identifiers should often remain strings, and generic decoding may use float64 unless configured otherwise.

Test real payload variations

Unmarshal multiple production-like examples, including missing fields, nulls, empty arrays and large numeric values before relying on generated structs.

Common questions

Frequently asked questions

Why use pointers in generated Go structs?

Pointers can distinguish an absent or null value from a type's zero value, when the contract requires that distinction.

Can JSON numbers overflow Go fields?

Yes. Select field types based on documented ranges and consider strings or json.Number for uncertain or very large values.