JSON to Go: structs you can json.Unmarshal into
Paste a JSON response or config file and get Go structs with json tags, ready for encoding/json.
Paste or upload your JSON
Put a JSON object, or a list of objects, into the left editor. You can also upload a .json file or click Sample to see the output first. Any valid JSON works; the more realistic the sample, the better the types, because every value is looked at.
Example input
{
"server": {
"id": 42,
"name": "web-server-01",
"hostname": "api.example.com",
"port": 8080,
"isActive": true,
"tags": ["production", "api", "critical"],
"config": {
"maxConnections": 1000,
"timeout": 30,
"enableLogging": true
}
}
}Check the generated structs
The structs appear on the right about half a second after you stop typing. Each nested object gets its own named struct, and a short note above the code tells you about anything that needed special handling, such as numbers longer than 64 bits. This is exactly what the example above turns into:
Generated Go
package main
// Decode with encoding/json: var v Root; err := json.Unmarshal(data, &v)
type Root struct {
Server Server `json:"server"`
}
type Server struct {
ID int `json:"id"`
Name string `json:"name"`
Hostname string `json:"hostname"`
Port int `json:"port"`
IsActive bool `json:"isActive"`
Tags []string `json:"tags"`
Config Config `json:"config"`
}
type Config struct {
MaxConnections int `json:"maxConnections"`
Timeout int `json:"timeout"`
EnableLogging bool `json:"enableLogging"`
}Lists are handled a little differently. Here every item is read, the long ID keeps an integer type, the zip code stays text because it was quoted, and the note that only one item has becomes a pointer:
[
{"id": 8901240544102066246, "zip": "00501", "price": 14.50},
{"id": 7, "zip": "10001", "price": 3, "note": "gift"}
]package main
// Decode with encoding/json: var items []Root; err := json.Unmarshal(data, &items)
type Root struct {
ID int64 `json:"id"`
Zip string `json:"zip"`
Price float64 `json:"price"`
Note *string `json:"note"`
}Copy or download the .go file
Copy the code or download it as types.go and drop it into your package. Change package main to your own package name if you need to. The structs work with the standard encoding/json package, and with anything built on it, such as the JSON binding in Gin or Echo.
Got a JSON Schema instead of sample data? JSON Schema to Go reads the schema's required fields and formats. For YAML config files there is YAML to Go, and if the JSON doesn't parse, run it through the JSON validator first.
What is JSON to Go Conversion?
Go reads JSON into structs: you describe the shape of the data once, and json.Unmarshal fills in the fields by matching each JSON key to a struct tag. Writing those structs by hand for a large API response is slow and easy to get wrong, especially the types of numbers and the fields that are only sometimes there.
This page writes them for you from a real example. It names the fields the way Go code usually does (UserID, not UserId), keeps the original keys in the tags, and picks types from the values it sees, so a 19-digit ID ends up as int64 rather than a float that would quietly round it.
Frequently Asked Questions
How does it pick a Go type for each number?
It looks at the number exactly as you typed it, not at a rounded JavaScript value. Whole numbers that fit in 32 bits become int, bigger ones become int64, positive values above the int64 range become uint64, and anything longer than 64 bits becomes json.Number so no digit is lost. A long ID such as 8901240544102066246 therefore gets int64. A number with a decimal point or an exponent (14.50, 1.0, 1e3) becomes float64. Text in quotes, like "00501", stays a string.
What happens when the items in a list have different fields?
Every item is read, not just the first one. A field that only some items have, or that is null in some of them, becomes a pointer such as *string, so a missing value stays nil instead of looking like an empty string or a zero. If one item has 3 and another has 14.5, the field becomes float64. If the kinds really clash (text in one item, a number in another), the field becomes interface{}.
How are JSON keys turned into Go field names?
Keys become exported Go names in PascalCase, with the common Go initialisms kept in capitals, so user_id becomes UserID and url becomes URL. Keys that start with a digit get an X in front, and two keys that end up with the same name get a number added (FirstName, FirstName2). The original key is always kept in the json tag, so encoding/json still matches it. A few keys can't be written in a json tag at all, such as an empty key or one containing a comma or a quote. Those fields are tagged json:"-" and the note above the code lists them.
What if my JSON is a list instead of an object?
Paste it as it is. The Root struct then describes one item of the list, built from all of the items, and the comment at the top shows how to decode the whole file into a slice: var items []Root. A list of plain values (numbers or strings) has no fields to describe, so the page tells you to decode it straight into something like []string.
Does the generated code compile as it is?
It should. The output is plain Go that only uses the standard library, and it imports encoding/json only when a field needs json.Number. We tested it by running gofmt and go vet on the output and decoding the original JSON back into the structs with json.Unmarshal. It does not add validation tags, database tags or omitempty, so add those yourself if your project needs them.
Is my JSON uploaded anywhere?
No. The structs are built in your browser, and the JSON is not sent to our server. Nothing is kept either: reload the page and the editor is empty again.
Related Tools
JSON Schema to Go
Generate Go types from JSON Schema with validation
YAML to Go
Convert YAML configuration to Go structs with yaml tags
TOML to Go
Convert TOML configuration to Go config structs
XML to Go
Convert XML data to Go structs with xml tags
CSV to Go
Convert CSV data to Go structs with field mapping
TypeScript to Go
Convert TypeScript interfaces and types to Go structs