About the JSON to Go
Generate Go struct definitions with json tags from any JSON sample. Field names follow Go conventions, including initialisms like ID and URL, and the output is aligned like gofmt.
Key features
- Go structs with json:"…" tags for every field
- Idiomatic names with initialisms: ID, URL, API, HTTP
- Pointers (*T) for nullable values
- omitempty for fields missing from some items
- Columns aligned like gofmt
- Custom package name
Go structs from JSON
Decoding JSON in Go means writing structs with exported fields and json tags. Doing that by hand for a large API response is tedious. Paste the JSON and get ready-to-use structs that follow Go conventions.
Example
[
{ "user_id": 7, "avatar_url": "https://…", "nickname": "ada", "roles": ["admin"] },
{ "user_id": 8, "avatar_url": "https://…", "nickname": null, "roles": [] }
]
type Root struct {
UserID int64 `json:"user_id"`
AvatarURL string `json:"avatar_url"`
Nickname *string `json:"nickname"`
Roles []string `json:"roles"`
}
var items RootList // the JSON is an array, so a RootList []Root type is generated too
if err := json.Unmarshal(data, &items); err != nil {
return err
}
Type mapping
| JSON | Go |
|---|---|
"text" | string |
42 | int64 |
3.14 | float64 |
true | bool |
[...] | []T |
{...} | named struct |
| null | *T |
| missing in some items | *T + ,omitempty |
| unknown | any |
Tips
- Time values: change ISO date strings to
time.Time;encoding/jsonparses RFC 3339 automatically. - Large numbers: use
json.Numberif you need exact precision beyond float64 for decimals. - Strict decoding:
decoder.DisallowUnknownFields()catches unexpected keys.
Related tools
Generate the client side with JSON to TypeScript, or another systems language with JSON to Rust.
How to use the JSON to Go
- Paste your data into the input box, upload a file, or click Sample to try it out.
- Adjust the options above the editor if you need to (indentation, delimiter, and so on).
- The Go appears instantly in the output panel. Any errors are shown with their line and column.
- Click Copy to copy the result, or Download to save it as a file.