How to convert JSON to Go
- Paste your JSON into the left panel, or click Load Sample.
- Go is already selected as the output on the right.
- Click Convert, then Copy Output.
Example
This JSON:
{ "id": 101, "name": "Ada Lovelace", "email": "ada@example.com", "active": true, "balance": 250.75,
"address": { "city": "London", "postcode": "N1 9GU" }, "tags": ["admin", "beta"] } becomes:
package model
type Root struct {
Id int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Active bool `json:"active"`
Balance float64 `json:"balance"`
Address Address `json:"address"`
Tags []string `json:"tags"`
}
type Address struct {
City string `json:"city"`
Postcode string `json:"postcode"`
} How JSON types map to Go
| JSON value | Go type |
|---|---|
"text" | string |
101 | int64 |
250.75 | float64 |
true | bool |
{ … } | a new struct named after the key |
["a", "b"] | []string |
Decoding JSON into the struct
var root model.Root
if err := json.Unmarshal(data, &root); err != nil {
return err
}
fmt.Println(root.Address.City) Go only decodes into exported fields (capitalised names), which is why every field starts with a capital letter and the original key sits in the tag.
Optional fields and arrays of objects
Paste an array of objects instead of a single object and the generator compares all items: fields missing from some items become pointers with omitempty, so you can tell "absent" from a zero value.
Things to check after generating
- Go style prefers
IDoverId; rename initialisms if your linter asks. - Rename
Rootand changepackage modelto your package. - Use
time.Timefor RFC 3339 timestamps if you want them parsed as times.
Is my JSON sent anywhere?
The code is generated in your browser; your JSON never leaves your computer. See the privacy policy.
Frequently asked questions
How do I convert JSON to a Go struct?
Paste the JSON on the left, keep Go selected on the right and click Convert. Copy the structs into your package and use json.Unmarshal to decode.
Why are the field names capitalised?
encoding/json can only set exported fields, and exported names start with a capital letter. The json tag keeps the original key name.
How are optional fields handled?
If you paste an array of objects, fields missing from some items become pointer types with omitempty in the tag.
Is my JSON uploaded?
No. Go structs are generated in your browser.
Other languages: JSON to Java · JSON to TypeScript · JSON to Python · JSON to C# · JSON to Kotlin · all languages