JSON to Go Struct
Paste JSON and get Go structs with json struct tags for every nested object, ready for encoding/json. Free, no signup, and generated in your browser so the payload is never uploaded.
16 lines · processed in your browser, nothing is uploaded
Go output · root.go
Generated code appears here.Example: JSON to Go
This JSON sample, with the root type named User, produces the Go below using the default options.
{
"id": 1024,
"name": "Ada Lovelace",
"email": "ada@example.com",
"active": true,
"score": 98.5,
"tags": [
"admin",
"beta"
],
"address": {
"city": "London",
"postcode": "NW1 6XE"
}
}package main
type User struct {
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Active bool `json:"active"`
Score float64 `json:"score"`
Tags []string `json:"tags"`
Address Address `json:"address"`
}
type Address struct {
City string `json:"city"`
Postcode string `json:"postcode"`
}
How to convert JSON to Go
- 1Paste a JSON object or array into the input.
- 2Select Go as the output language.
- 3Name the root struct, for example Order.
- 4Adjust options such as the package name if needed.
- 5Copy the structs into a .go file and unmarshal with json.Unmarshal.
Struct tags map JSON keys to Go fields
Go fields must be exported (capitalized) for encoding/json to see them, so a key like user_id becomes a field such as UserID with the tag `json:"user_id"`. The tag keeps the wire name exact while the Go name follows Go conventions.
Keep tags when renaming fields. Changing the field name is safe; changing the tag changes what JSON you read and write.
omitempty and pointers for optional fields
`json:"name,omitempty"` drops the key when marshaling if the value is the zero value: empty string, 0, false, nil pointer, or an empty slice or map. On its own it cannot tell "missing" from "zero".
Use a pointer such as *int when you need that distinction: nil means the key was absent or null, while a pointer to 0 means an explicit zero. Plain values are simpler and fine when zero is never a meaningful value.
Numbers, nulls and mixed types
JSON has one number type, so the generator picks int64 or float64 from your sample. If a field is 1 in your sample but 1.5 in production, unmarshaling fails, so check numeric fields against real data.
Fields whose type varies between records may come out as interface{} (any). Replace those with a concrete type or a custom UnmarshalJSON method once you know the real shape.