Code generator
JSON to Go Struct
Paste a JSON example, such as an API response, and get Go structs with the right json tags, ready for encoding/json.
How to generate Go structs
- Paste a real JSON example, or press Sample.
- Set the root type name. Nested objects get their own structs, named after their keys.
- Copy the code or download
types.go, then unmarshal withjson.Unmarshal(data, &v).
How JSON maps to Go
| JSON | Go |
|---|---|
| string | string |
| whole number | int64 |
| decimal number | float64 |
| true / false | bool |
| object | a named struct |
| array | []T |
| value that can be null | pointer, e.g. *string |
| mixed types, or only null | any |
Field names and tags
Go only encodes and decodes exported fields, which start with a capital
letter. So user_name becomes UserName, and a struct tag such as
`json:"user_name"` keeps the original key. Common initialisms follow Go style:
id becomes ID and url becomes URL.
When objects in an array have different keys, every key is included, and keys that are
missing from some objects get omitempty so they are left out when you encode
the struct again.
Frequently asked questions
Why are the field names capitalized?
encoding/json only reads and writes exported fields, and in Go a field is exported when its name starts with a capital letter. The json tag keeps the original key name.
Why is a field a pointer?
The value was null in the example. A pointer can be nil, so you can tell a missing or null value apart from an empty string or zero.
How are dates typed?
As string, because JSON has no date type. If the value is RFC 3339, you can change the type to time.Time, which encoding/json parses automatically.
What does any mean?
any is an alias for interface{} (Go 1.18+). It is used when a field had several different types, or only null, in the example.
Is my data uploaded?
No. Everything runs with JavaScript inside your browser tab. Nothing you paste is sent to a server or stored.