JSON から Go 構造体へ
JSON を json タグ付きの Go 構造体に変換し、gofmt と同じように整列します。省略可能なフィールドは omitempty 付きのポインターになり、タイムスタンプは time.Time にできます。
*string tells “missing or null” apart from “”
Off uses int
RFC 3339 strings like 2026-03-14T09:26:53Z
package main
import "time"
type Root struct {
ID int64 `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
IsActive bool `json:"isActive"`
Rating float64 `json:"rating"`
CreatedAt time.Time `json:"createdAt"`
AvatarURL any `json:"avatarUrl"`
Tags []string `json:"tags"`
Address Address `json:"address"`
Orders []Order `json:"orders"`
}
type Address struct {
Street string `json:"street"`
City string `json:"city"`
ZipCode string `json:"zipCode"`
}
type Order struct {
OrderID string `json:"orderId"`
Total float64 `json:"total"`
Items int64 `json:"items"`
Shipped bool `json:"shipped"`
Coupon *string `json:"coupon,omitempty"`
}
Arrays of objects are merged: a key missing from some items becomes optional, null makes a field nullable, and identical shapes share one type. Paste several examples in an array for the most accurate types.
このツールについて
JSON を貼り付けると、そのままコードに組み込める Go 構造体が得られます。フィールド名は ID、URL、API などの頭字語を含む Go の命名規則に従い、すべてのフィールドに json タグが付き、出力は gofmt で整形した場合とまったく同じように整列されます。オブジェクトの配列はマージされるため、一部の要素にしかないキーは省略可能(omitempty 付きのポインター)になり、null はフィールドを nullable にし、10.0 のような値でも整数と小数を区別します。ネストしたオブジェクトはそれぞれ名前付きの型になり、同じ形のものは 1 つの型を共有し、ID をキーにしたオブジェクトは map になります。オプションで、パッケージ名、ポインター、omitempty、int または int64、RFC 3339 タイムスタンプ用の time.Time を選べます。同じページから Rust、C#、Python、Kotlin に切り替えることもできます。
JSON を Go 構造体に変換する方法
JSON サンプルから Go の型を生成します。
- 1
JSON を貼り付け
JSON オブジェクトを貼り付けます。最も正確な型を得るには、例の配列を貼り付けてください。
- 2
オプションを選ぶ
ルート型の名前、パッケージ、ポインター、omitempty、time.Time を設定します。
- 3
コードをコピー
構造体をコピーするか、types.go をダウンロードします。