JSON to Go Struct Generator
Paste JSON and get Go struct definitions with json tags, generated locally in your browser.
Paste JSON and get Go struct definitions with json tags, generated locally in your browser.
Paste a JSON document and click Generate to get Go type definitions you can drop straight into a package. Each object becomes an exported struct, each key becomes an exported field in PascalCase, and a `json:"..."` tag keeps the original key so encoding/json can Unmarshal the data directly. The fields are aligned the way gofmt aligns them.
package main..go file, change the package name and run gofmt.json.Unmarshal(data, &root) and adjust any types the sample could not pin down.| JSON | Go |
|---|---|
| whole number | int64 |
| decimal, or mixed whole and decimal | float64 |
| string / boolean | string / bool |
| object | Named struct type |
| array | Slice such as []string or []Order |
sometimes null | Pointer such as *float64, so nil is distinct from 0 |
| missing from some array items | Tag gets ,omitempty |
only ever null | interface{} |
{ "items": [
{ "id": 1, "note": "a", "score": null },
{ "id": 2, "score": 3.5, "created_at": "2026-01-01" } ] }
becomes:
type Root struct {
Items []Item `json:"items"`
}
type Item struct {
Id int64 `json:"id"`
Note string `json:"note,omitempty"`
Score *float64 `json:"score"`
CreatedAt string `json:"created_at,omitempty"`
}
Then var r Root; err := json.Unmarshal(data, &r). The array items was merged across both elements and named with its singular, Item.
package main; change it to your package name.ID and URL; the generator writes Id and Url. Rename them if your linter complains; the tag keeps the JSON mapping correct.string. Change the field to time.Time if values are RFC 3339.int64 is safe in Go, but check the producer: JavaScript producers may already have rounded them. See JSON big integer precision.[{"sku":"A1","qty":2,"user-id":5,"2fa":true}]generates
package main
// The top-level value is an array; the classes below describe its elements.
type Root struct {
Sku string `json:"sku"`
Qty int64 `json:"qty"`
UserId int64 `json:"user-id"`
N2fa bool `json:"2fa"`
}The comment marks that the JSON root is an array, so decode into []Root. Keys with dashes keep their exact spelling in the tag, and a key starting with a digit gets an N prefix to form a legal Go identifier.
{"id":12345678901234567890,"ratio":1.0,"values":[1,"a"],"tags":[]}generates
package main
type Root struct {
Id int64 `json:"id"`
Ratio int64 `json:"ratio"`
Values []interface{} `json:"values"`
Tags []interface{} `json:"tags"`
}Three things to fix by hand here. The ID is beyond the int64 range, so Unmarshal would fail; use uint64 or a string. 1.0 reaches the generator as the whole number 1, so ratio is typed int64, and a later value of 1.5 would be rejected with cannot unmarshal number 1.5 into Go struct field; change it to float64. The empty and mixed arrays become []interface{} (the same as []any since Go 1.18).
,string tag option or a custom type.omitempty only affects encoding with Marshal; it does not make decoding stricter. Use pointers when you must detect absence.user_id and userId both become UserId; the generator appends an underscore to the second so the code still compiles. Pick clearer names.DisallowUnknownFields() on a json.Decoder if you want extra keys to fail.Use this page when Go code consumes the JSON. For a contract shared with other languages, start with the JSON Schema generator; for the same payload on the frontend, use JSON to TypeScript; for .NET, JSON to C#. If the input will not parse, the JSON validator shows the exact line and column of the error.
The sample showed null for that key in at least one place. A pointer lets you tell a JSON null (nil) apart from a zero value.
No. Every nested object gets its own named type, which is easier to reuse and test than inline struct { ... } fields.
Field names are built by capitalising each word of the key, so id becomes Id and user_url becomes UserUrl. Rename them to ID and UserURL if golint asks; the json tag keeps the mapping correct.
The sample did not reveal a single type: the value was always null, the array was empty, or the array mixed types. Replace it with a concrete type, or json.RawMessage if you want to decode it later.
Values above 9223372036854775807 overflow int64 and Unmarshal returns an error. Use uint64 if they are never negative, or json.Number or a string if they can be anything.
No. Parsing and code generation run in your browser.