JSON to Go Struct Generator

Paste a JSON sample and get Go struct types with json tags, named and formatted the way gofmt and golint expect.

Loading tool…

About the JSON to Go

Each key becomes an exported field with a json:"…" tag that holds the original key. Names follow the Go conventions: user_id becomes UserID, avatarUrl becomes AvatarURL and image_urls becomes ImageURLs, using the initialisms of golint (ID, URL, HTTP, API, JSON, UUID…). A key that starts with a digit gets the prefix X, and two keys that give the same name get a number. The output is aligned in columns exactly as gofmt does.

Whole numbers become int (int64 beyond 32 bits), numbers with a fraction float64; 1.0 counts as a float. Arrays become slices, and the items of an array are merged into one type: a field that is a whole number in one item and a fraction in another is float64. A value that is null in some places is a pointer (*string); a field that is only ever null, or holds values of different kinds, is interface{} (or any). RFC 3339 strings such as 2026-09-28T17:42:09Z become time.Time, which encoding/json parses from that format.

A key that is missing from some items of an array is optional and gets omitempty. Optional structs and times are pointers, because omitempty never leaves out a struct value. Choose pointers for all optional fields when you need to tell a missing value from 0 or "". Nested objects are separate named types by default, with identical shapes declared once, or anonymous structs inside the parent. The types are inferred from one sample, so check the fields that were null or empty in it.

How to use it

  1. Paste a JSON sample or open a .json file.
  2. Set the root type name and choose named types or inline structs.
  3. Choose where omitempty goes and whether optional fields are pointers.
  4. Copy the Go code or download types.go.

Frequently asked questions

When should a field be a pointer?
When you must tell "missing or null" from the zero value. With a plain int, a missing field and 0 look the same after Unmarshal; with *int the pointer is nil when the field was missing or null.
What does omitempty do?
When the struct is encoded, the field is left out if it holds the zero value: false, 0, "", a nil pointer, or an empty slice or map. It has no effect on decoding, and it never omits a struct value.
Why interface{} for some fields?
The sample does not say which type the field has: it was null everywhere, the array was empty, or the values had different kinds. Replace it with the real type when you know it.
Why are names like Id changed to ID?
Go style writes initialisms in one case: userID, HTTPServer, URL. Linters such as golint and revive report UserId. The json tag keeps the original key, so the JSON does not change.

Related tools