Paste JSON and get Go structs with json tags as you type.
Paste JSON, upload a .json file or drop one here.
Updates as you type. Copy it or download models.go.
Converting…
This free JSON to Go struct converter reads a sample JSON document and writes the Go types that match it: exported fields, json tags, nested structs and slices. Paste an API response, a webhook payload or a config file and the code appears as you type, ready to paste into your package. Nothing is uploaded, because the structs are generated in your browser.
Writing these types by hand is slow and easy to get wrong, especially for payloads with dozens of fields or several levels of nesting. Generating them from a real sample gives you a correct starting point that you can then rename, trim and document. If you are looking for a quick way to go from JSON to Golang types without installing a CLI, this is it.
.json file. You can also drag a file onto the editor.models.go.Here is a small user record and the Go it produces. Field names are capitalized so they are exported, each one keeps its original key in a tag, and the nested address object and the orders array each get their own struct.
{
"user_id": 48213,
"username": "ada",
"balance": 1042.75,
"created_at": "2024-03-18T09:30:00Z",
"roles": ["admin", "editor"],
"address": {
"city": "London",
"postal_code": "N1 9GU"
},
"orders": [
{ "id": 1, "total": 59.9, "coupon": "SPRING" },
{ "id": 2, "total": 120 }
]
}package main
import "time"
type Root struct {
UserID int64 `json:"user_id"`
Username string `json:"username"`
Balance float64 `json:"balance"`
CreatedAt time.Time `json:"created_at"`
Roles []string `json:"roles"`
Address Address `json:"address"`
Orders []Order `json:"orders"`
}
type Address struct {
City string `json:"city"`
PostalCode string `json:"postal_code"`
}
type Order struct {
ID int64 `json:"id"`
Total float64 `json:"total"`
Coupon *string `json:"coupon,omitempty"`
}To decode JSON into these types, pass a pointer to json.Unmarshal. The generated structs work with the standard library as they are.
package main
import (
"encoding/json"
"fmt"
"log"
"os"
)
func main() {
data, err := os.ReadFile("user.json")
if err != nil {
log.Fatal(err)
}
var user Root
if err := json.Unmarshal(data, &user); err != nil {
log.Fatal(err)
}
fmt.Println(user.Username, len(user.Orders))
}| JSON | Go type | Notes |
|---|---|---|
| object | struct | Each nested object gets its own named struct. |
| array | []T | An empty array has no element type, so it becomes []interface{}. |
| string | string | ISO 8601 date-times become time.Time. |
| whole number | int64 | Fits any integer up to 64 bits. |
| number with a fraction | float64 | A field that mixes both, like 1.5 and 2, becomes float64. |
true / false | bool | Direct mapping. |
null | interface{} | Or a pointer such as *string when other objects show the real type. |
The encoding/json package only reads and writes exported fields, so every field name must start with an upper-case letter. The converter turns keys into idiomatic Go names: user_id becomes UserID, weird-key becomes WeirdKey and a key such as 1st becomes The1St. The tag, for example `json:"user_id"`, records the real key. It is what makes decoding and encoding work, since the Go name no longer matches the JSON key.
Adding ,omitempty to a tag makes json.Marshal skip the field when it holds a zero value: 0, false, an empty string, a nil pointer or an empty slice. The converter adds it by itself to fields that are missing from some objects in your sample, like coupon in the example. The omitempty on all fields checkbox adds it to every tag, which is handy for request bodies where unset fields should not be sent. Keep in mind that omitempty has no effect on struct values such as time.Time; Go 1.24 added an omitzero option for that.
Go values cannot be nil unless they are pointers, slices, maps or interfaces. When a field is null in one array element and a string in another, the output uses *string, so you can tell a missing value from an empty one. A field that is null in your only sample has no known type and becomes interface{}; replace it with the right type once you know it.
The types come from your sample, so use one that is representative. If a price is 10 in the sample but 10.5 in production, an int64 field fails to decode the second value, so include examples of both in an array and the converter picks float64. Date-times in RFC 3339 form decode into time.Time directly; plain dates such as 2024-03-18 stay strings and need parsing with time.Parse.
Working in more than one language? The same sample also converts to a JSON to TypeScript interface, a C# class or a Java class. Need sample data to try the converter on? Create some with the JSON Generator, or describe a document's shape with the JSON Schema generator. For wire formats compared, see Protobuf vs JSON.
Paste your JSON into the JSON Input editor, or upload a .json file. The Go structs appear in the output editor as you type. Copy them, or click Download to save models.go, then decode your data with json.Unmarshal.
Go only exports identifiers that start with an upper-case letter, and encoding/json ignores unexported fields. The converter capitalizes each name (user_id becomes UserID) and keeps the original key in the json tag so the field still matches your JSON.
Whole numbers become int64 and numbers with a fraction become float64. A field that is null in some objects of an array, such as a note that is null in one order and a string in another, becomes a pointer like *string. A null with no other example becomes interface{}.
The omitempty tag option tells json.Marshal to leave out a field when it holds a zero value such as 0, false, an empty string, a nil pointer or an empty slice. Tick omitempty on all fields to add it to every tag. Fields that are missing from some objects in your sample already get it automatically.
Strings in ISO 8601 date-time form, such as 2024-03-18T09:30:00Z, become time.Time and the output adds the time import. Date-only strings like 2024-03-18 stay string, because they are not valid RFC 3339 timestamps for time.Time.
No. The structs are generated entirely in your browser, so your JSON never leaves your device. That makes it safe to use with real API responses that contain private data.