Skip to content
Mediawand

JSON to Go Structs

Decode JSON in Go without typing out structs. Paste an example and get a struct for every nested object, with json tags, Go-style field names and sensible number types.

How to generate Go structs from JSON

  1. 1Paste a JSON example, ideally with several array items.
  2. 2Name the root struct and choose whether nullable fields are pointers.
  3. 3Copy the code or download types.go.

How the types are worked out

Every item of an array is merged into one type, so a key that's missing from some items becomes optional and a value that's sometimes null becomes nullable. Whole numbers and decimals are told apart, nested objects get their own named types (an “items” array gets an “Item” type), and objects with the same shape share one type.

Go specifics

Field names follow Go conventions, so user_id becomes UserID and api_url becomes APIURL. Whole numbers are int64, decimals float64, optional keys get omitempty, and values of mixed types become any.

Pointers for null

With pointers on, a field that's sometimes null becomes a pointer like *string, so you can tell null from an empty string. Slices and any already hold nil, so they stay as they are.

Frequently asked questions

Is my data uploaded?
No. Everything runs in your browser, so the JSON never leaves your device.
Why int64 instead of int?
int64 holds the same range on every platform, which matters for IDs and timestamps. Change it if you prefer int.
Does it add the package line?
No, so you can paste the structs into any file. Add package yourpackage at the top of a new file.

Related tools

Loading tools…