"category": "code-generator",
JSON to Go Struct Generator
"tldr": Paste JSON and get compilable Go structs - PascalCase fields, json tags with original keys, int64/float64 types, nested structs.
Paste a JSON payload and get Go struct definitions ready for encoding/json. Field names are converted to exported PascalCase, original JSON keys are preserved in json tags, integers and floats map to int64 and float64, and nested objects become their own named structs.
This eliminates the most error-prone part of consuming an API from Go: hand-writing struct tags. A single typo in a json tag silently produces zero values instead of an error - generating the structs from a real response avoids that class of bug entirely.
How to convert JSON to Go structs
- 1Paste a representative JSON payload - a real API response gives the best types.
- 2Nested objects become separate named structs, defined before the structs that use them.
- 3Every field gets a json tag with the original key, so unmarshaling works even for snake_case keys.
- 4Copy the structs into your Go file and unmarshal with json.Unmarshal.
Convert JSON to Go struct in code
var order Root
if err := json.Unmarshal(body, &order); err != nil {
log.Fatal(err)
}
fmt.Println(order.Customer.Name, order.Total)type Root struct {
Notes *string `json:"notes,omitempty"` // nil when absent or null
}Frequently asked questions
Why are null fields typed as interface{}?
A null in the sample gives the generator no type information, so it falls back to interface{} (any in modern Go). If you know the real type, change it to a pointer like *string - pointers are the idiomatic Go way to represent nullable JSON fields.
Why int64 and float64 instead of int and float32?
encoding/json decodes JSON numbers into float64 by default, and int64 safely holds any integer JSON can reasonably carry. Using the 64-bit types avoids silent truncation on large IDs and timestamps - narrow them deliberately if you know the range.
What happens if my field names collide after conversion?
Keys like "user_name" and "userName" both convert to UserName. The generator appends a numeric suffix to keep the struct compiling; rename the fields afterward - the json tags keep unmarshaling correct regardless of the Go-side names.
How do I handle optional fields?
Add ",omitempty" to the tag for marshaling (skips zero values) and use pointer types for fields that may be absent or null when unmarshaling, so you can distinguish "missing" from "zero".
Last updated:
You might also need
Free and in-browser, like everything on JSON Console. Browse all tools →