What it does
The JSON to Go converter reads a JSON sample and writes Go struct definitions with json tags, ready for encoding/json. Paste a response from the API you are calling and get types you can use with json.Unmarshal right away, instead of writing every field and tag by hand.
The converter inspects every value in the sample. Nested objects become their own structs, all items of an array are merged into one element type, and fields that are missing from some items or sometimes null are detected as optional. The output is laid out the way gofmt would format it.
How to use
- Paste JSON into the input, upload a
.jsonfile, or click Sample. - Set the root type name and choose how nested objects are written, which fields get
omitempty, and whether nullable fields use pointers. - Copy the Go code or download it as a
.gofile, then add yourpackageline.
Invalid JSON is reported with the line and column of the problem.
Example
This JSON:
{ "id": 7, "user_name": "ada", "tags": ["admin"], "profile": { "avatar_url": "https://example.com/a.png" } }
becomes these structs:
type Root struct {
ID int `json:"id"`
UserName string `json:"user_name"`
Tags []string `json:"tags"`
Profile Profile `json:"profile"`
}
type Profile struct {
AvatarURL string `json:"avatar_url"`
}
Tips
- One sample only shows what it contains. If a value can be a number or a string, include both, or put several samples in an array so they are merged.
- Timestamps arrive as strings. Change the type to
time.Timeyourself when the format is RFC 3339. - Objects with exactly the same fields share one struct, which keeps the output short when the same shape appears in several places.
- Writing TypeScript too? The JSON to TypeScript generator uses the same inference to produce interfaces.
FAQ
› How are field names chosen?
Each JSON key is turned into an exported Go name in PascalCase, with common initialisms in capitals the way golint expects, so user_id becomes UserID and avatar_url becomes AvatarURL. The original key is kept in the json tag, so encoding/json maps the fields exactly.
› Which Go types are used?
Strings become string, whole numbers become int (or int64 when a value does not fit in 32 bits), decimals become float64 and booleans become bool. Arrays become slices, empty objects become map[string]any, and values that are only null or mix several kinds become any.
› What is the difference between named and inline structs?
Named mode writes every nested object as its own type, such as Address or Order, which is easier to reuse and test. Inline mode puts anonymous struct types directly inside the parent, which keeps everything in one declaration, the way many quick converters do.
› When is omitempty added?
By default only to optional fields, meaning fields missing from some objects in an array or sometimes null. You can also add it to every field or to none. Turn on pointers to tell a missing or null value apart from a zero value.
› Is my JSON uploaded?
No. The structs are generated in your browser and nothing is sent to a server.