JSON to Go Struct
Generate Go structs with json tags, omitempty and pointers from a JSON sample.
About this tool
Paste a JSON response or config. Go structs appear as you type, ready for encoding/json. Set the root struct name and an optional package, choose named or inline nested structs, and turn omitempty and pointer types on or off. Copy the result into your project.
How JSON maps to Go types
Go's encoding/json decodes into structs whose exported fields carry json:"โฆ" tags naming the original keys. This tool infers those structs from a sample. Strings become string, whole numbers int64, decimals float64, true/false bool, arrays slices such as []string, and nested objects their own named structs. Field names follow Go's style guide, including initialisms, so user_name becomes UserName, avatar_url becomes AvatarURL and order_id becomes OrderID. Objects inside an array are merged, so the struct covers every item. A key that only some items have gets ,omitempty, a value that's sometimes null becomes a pointer such as *string (so you can tell missing from empty), and a field that's null in every item becomes any. If an array mixes whole numbers and decimals, it becomes float64. The output is aligned the way gofmt would align it. Honest limits: types come from your sample, not the API's contract. Generate from the most complete response you have, and check numbers that could be decimals or values that could be null. JSON to TypeScript, JSON to Python and the JSON formatter are related, and What JSON is, and where it trips people up covers the basics.
Frequently asked questions
How do I convert JSON to a Go struct?
Paste the JSON here. Each object becomes a struct with exported, correctly cased fields and json tags, which you can decode with json.Unmarshal.
Why does a field become a pointer?
The value was null in at least one item. A pointer such as *string lets your code tell null apart from an empty string.
Why is the field called OrderID, not OrderId?
Go's naming convention keeps initialisms like ID, URL and API in capitals, and golint expects this.
Is my JSON sent anywhere?
No. It's processed in your browser and nothing is stored or sent.