JSON to Go struct: from JSON to code
Paste JSON and get Go structs with the json tags that encoding/json needs. Field names become exported names in CamelCase (user_id becomes UserID, with Go's usual acronym style), each tag keeps the original key, whole numbers are int, decimals float64, and a field that is missing in some items gets a pointer or omitempty so that the zero value is not confused with absence.
This page opens the converter on Go. Paste your JSON, choose the root name and the flavour, and copy the result; everything is generated in your browser and the JSON is never sent anywhere.
A real conversion
The tool opens with this JSON, an order with a customer and two items:
{
"id": 48213,
"status": "paid",
"paid_at": "2026-11-21T14:03:00Z",
"total": 389.3,
"coupon": null,
"customer": { "name": "Ana Souza", "email": "ana@example.com", "vip": true },
"items": [
{ "sku": "KB-01", "qty": 2, "price": 149.9, "tags": ["usb", "keyboard"] },
{ "sku": "MS-07", "qty": 1, "price": 89.5 }
]
}and this is what it writes for Go:
type Root struct {
ID int `json:"id"`
Status string `json:"status"`
PaidAt string `json:"paid_at"`
Total float64 `json:"total"`
Coupon any `json:"coupon"`
Customer Customer `json:"customer"`
Items []Item `json:"items"`
}
type Customer struct {
Name string `json:"name"`
Email string `json:"email"`
Vip bool `json:"vip"`
}
type Item struct {
Sku string `json:"sku"`
Qty int `json:"qty"`
Price float64 `json:"price"`
Tags []string `json:"tags,omitempty"`
}How each JSON value is typed
| In the JSON | In Go |
|---|---|
| string | string |
| integer | int |
| decimal | float64 |
| true / false | bool |
| null | any |
| array of items | []Item |
| field missing in some items | `json:"tags,omitempty"` |
The second item of the sample has no "tags" key, and the first has one: that is why the tool treats it as optional.
Awkward keys, null and empty lists
{"user_id": 7, "display name": "Ana", "tags": [], "address": null, "scores": [9.5, 8], "meta": {"2fa": true}, "mixed": [1, "a"]}type Account struct {
UserID int `json:"user_id"`
DisplayName string `json:"display name"`
Tags []any `json:"tags"`
Address any `json:"address"`
Scores []float64 `json:"scores"`
Meta Meta `json:"meta"`
Mixed []any `json:"mixed"`
}
type Meta struct {
F2fa bool `json:"2fa"`
}The key "display name" keeps its tag and becomes DisplayName; the key "2fa" cannot start a Go name, so it becomes F2fa with the original tag; an empty array and null become []any and any, because there is nothing to infer from them.
The other flavour
The same small JSON, {"items": [{"id": 1, "note": "a"}, {"id": 2}]}, in the default flavour and in the "values" one:
type Order struct {
Items []Item `json:"items"`
}
type Item struct {
ID int `json:"id"`
Note *string `json:"note,omitempty"`
}type Order struct {
Items []Item `json:"items"`
}
type Item struct {
ID int `json:"id"`
Note string `json:"note,omitempty"`
}With the "values" flavour optional fields stay plain values with omitempty instead of pointers: simpler to use, but a real 0 or "" cannot be told from a missing field.
Decoding the JSON into the struct
var order Root
if err := json.Unmarshal(data, &order); err != nil {
return err
}
fmt.Println(order.Customer.Name)Unmarshal ignores keys that have no field, and leaves fields that are not in the JSON with their zero value. Use json.NewDecoder(r).DisallowUnknownFields() when you want strictness.
Common mistakes
- A lower-case field name: encoding/json only fills exported fields. The tool always writes exported names, so keep them capitalised if you rename.
- Big numbers: a JSON integer above 2^53 loses precision if it passes through float64, such as when decoded into any. Type the field as int64 or json.Number.
How to use the converter
- Paste your JSON on the left (or keep the example); a mistake is reported with its line and column.
- Set the root name and the flavour; the code updates as you type.
- Copy the code or download it as a file.
Frequently asked questions
How do I convert JSON to a Go struct?
Paste the JSON, choose the root name and the pointer or value flavour, and copy the structs. Then json.Unmarshal fills them.
Why does the tool use pointers for some fields?
A pointer can be nil, which says "the key was absent", something a plain int or string cannot say. Switch to the values flavour if you do not need the distinction.
What about field names that are not valid in Go?
The Go name is derived (spaces and punctuation removed, a letter prefix added if it starts with a digit) and the json tag keeps the exact original key, so decoding still works.