Guide
Generating Go structs from JSON
Go’s encoding/json maps JSON onto structs, which means every API you call needs a set of struct definitions with the right types and tags. Writing them by hand for a large response is tedious, and it is easy to choose the wrong number type or forget which fields can be missing. Paste a sample response here and get idiomatic structs you can drop into a package.
Idiomatic names
Go exports fields that start with a capital letter, so every JSON key becomes a PascalCase field name. Common initialisms follow Go style rather than plain capitalisation: id becomes ID, url becomes URL, html_url becomes HTMLURL and order_id becomes OrderID, which is what golint and most Go codebases expect. If two keys would end up with the same name, the second gets a number.
Struct tags
Because Go names differ from the JSON keys, every field carries a tag with the original key: json:"order_id". Tags also control output when you marshal the struct back to JSON. Fields that were missing or null in at least one sample get ,omitempty, so encoding leaves them out instead of writing a zero value.
Pointers or zero values
In Go, a missing int field decodes as 0 and a missing string as “”, which you cannot tell apart from a real zero or empty string. By default, optional and nullable fields become pointers — *string, *int64, *Address — so nil means absent. Switch pointers off for simpler code when the difference does not matter to you. Slices and any are never pointers, because nil already means absent for them.
Choosing the number types
Whole numbers become int64 and decimals float64, safe for any value JSON can carry. If a field holds whole numbers in some samples and decimals in others, it becomes float64. Narrow types by hand where you know the range, for example int for a page count.
Dates and unknown values
Switch on date types and ISO 8601 timestamps become time.Time, which encoding/json parses from RFC 3339 strings; the time import is added for you. Values that are always null, or that change type between samples, become any, the alias for interface{}.
A worked example
From the sample user record, the generator writes four structs: User, Order, Item and Address. The order’s key order_id becomes OrderID int64 \json:“order_id”`, the coupon that appears in only one order becomes Coupon *string `json:“coupon,omitempty”`, and the address’s always-null unitbecomesUnit any`.
After generating
Run gofmt to align the tags in your editor’s style, rename the root type to something meaningful, and add the package to your module. If the API publishes an OpenAPI document, compare the structs against it and tighten any field that the samples happened to leave empty.
Arrays at the root and JSON Lines
If the response is an array, the generated struct describes one element; decode into a slice with var users []User. Logs and exports in JSON Lines format can be pasted as they are, one document per line: all lines are merged into the same structs, which is a quick way to find the fields that only appear now and then.
Related tools
Generate TypeScript types for the frontend with JSON to TypeScript, structs for another backend with JSON to Rust, Go request code with cURL to Code, or inspect the input with the JSON Formatter.
Checked against the current version of the JSON to Go Struct on .

