Sheet 02 Go structs
JSON to Go
Paste JSON samples and get Go structs with encoding/json tags, gofmt-aligned and ready to paste into a package.
package model
type Root struct {
Event string `json:"event"`
ID string `json:"id"`
Created string `json:"created"`
Livemode bool `json:"livemode"`
Data Data `json:"data"`
}
type Data struct {
OrderID int64 `json:"orderId"`
Total float64 `json:"total"`
Currency string `json:"currency"`
Customer Customer `json:"customer"`
Items []Item `json:"items"`
}
type Customer struct {
ID int64 `json:"id"`
Email string `json:"email"`
}
type Item struct {
SKU string `json:"sku"`
Qty int64 `json:"qty"`
Note *string `json:"note"`
}
1 sample · 4 types
How it works
Each sample is read as a value of the root type; keys missing from some objects are optional, and values that are sometimes null are nullable. Field names follow Go conventions, initialisms included (userId becomes UserID), and every field keeps its JSON key in a tag.
- omitempty and omitzero
- Optional keys get
omitempty. Optional structs andtime.Timevalues getomitzero(Go 1.24), becauseomitemptynever omits a struct. - Pointers
- Nullable values become
*stringand the like, so null and the zero value stay different. Slices, maps andanyare never pointers: they can already be nil. - time.Time
- Only strings in RFC 3339 form with a time zone, which is what
time.Timecan decode. - Mixed types
- Values that are sometimes a number and sometimes a string become
any, with a comment listing what was seen.
Questions
Why is a key skipped with a comment?
Go's encoding/json can only name keys made of letters, digits and a few punctuation marks in a struct tag. A key containing a comma, quote, backslash or a line break cannot be mapped, so it is listed in a comment instead of producing a field that would never fill.
Why int64 rather than int?
JSON numbers have no size, so int64 is the safe choice on every platform. Integers larger than int64 become uint64 when they are positive, or float64 with a comment.
Is my JSON uploaded?
No. The code is generated by JavaScript running in this tab, and nothing you paste leaves it. The site counts page views with Cloudflare Web Analytics, which sets no cookies and never receives what you paste.