How to use the JSON to Go Struct
- 1
Paste JSON
A webhook body, a REST response or a config file — an object or an array of objects.
- 2
Name the root struct
Replace Root with a name such as WebhookEvent.
- 3
Generate
Structs appear with aligned fields and struct tags, parent first, followed by nested types.
- 4
Use in your package
Add a package line, save as a .go file and run json.Unmarshal or a json.Decoder.
Features
Exported fields
Every field starts with a capital letter, which encoding/json requires to see it.
Go initialisms
Words like id, url, api, http, ip and uuid are upper-cased, so event_id becomes EventID and html_url becomes HTMLURL.
Pointers for optional values
Nullable or sometimes-missing scalars become *int, *string and so on, with omitempty in the tag.
int or int64
Integers that exceed 32 bits in your sample get int64 explicitly.
gofmt-style alignment
Field names, types and tags are column-aligned with tabs.
Browser-only
Nothing is uploaded.
Who uses it — and for what
Webhook handlers
Model Stripe-, Razorpay- or GitHub-style events before writing the HTTP handler.
Cloud and DevOps tools
Parse output from cloud CLIs and APIs in Go automation.
Microservices
Define request and response types for net/http, Gin or Echo services.
Go newcomers
Learn how struct tags and pointers express JSON shapes.
How the types are inferred
The generator reads your sample and infers a type for every field: strings, integers and decimals, booleans, nested objects (each gets its own named Go type) and arrays. If an array holds several objects, all of them are merged — fields missing from some items become optional and fields that are sometimes null become nullable, so one representative response is usually enough. Field names that aren’t valid identifiers are converted and mapped back to the original JSON key.
Using the Go code
Each struct field is exported (PascalCase) with a json:"key" tag so encoding/json maps it to the original name. Nullable and optional fields use pointers (*string) with omitempty, so you can tell “missing” from the zero value. Whole numbers become int (or int64 when the sample exceeds 32 bits) and decimals float64. Use it with json.Unmarshal(data, &root).
Everything runs in your browser — your code and data are never uploaded, so it is safe to paste private config files and API responses.
How encoding/json matches these structs
Go’s standard JSON package uses reflection and the struct tags to move data in and out. Understanding its rules explains the generated code and helps you adjust it:
- Only exported fields count. A lower-case field is invisible to
encoding/json, which is why every name is capitalised. - Key matching is case-insensitive on input. When unmarshalling, Go prefers an exact tag match but will accept a key that differs only in case. The tags still matter for output, where the exact name is written.
- Unknown keys are ignored. Call
dec.DisallowUnknownFields()on ajson.Decoderif you want extra fields to be an error. - omitempty works on output only. It drops a field from
json.Marshalwhen the value is zero —nilfor pointers,0,"",false, or an empty slice or map. It has no effect when reading.
Pointers, zero values and when to change the output
In Go every type has a zero value, so a plain int field cannot tell you whether the JSON said 0 or said nothing. The generator uses a pointer when your sample proves a field can be null or missing; nil then means “absent”. For required fields it keeps plain values, which are easier to use.
| Situation | Generated | Possible change |
|---|---|---|
| Optional number | *int | Keep, or use int if 0 and “missing” mean the same to you |
| Optional list | []string with omitempty | Nothing — a nil slice already means absent |
| Unix timestamp | int | int64, then time.Unix(v, 0) |
| ISO date string | string | time.Time, which parses RFC 3339 automatically |
| Unknown value | interface{} | A concrete type, or json.RawMessage to decode later |
On 64-bit platforms Go’s int is already 64 bits wide, but the language only guarantees at least 32, so explicit int64 is the portable choice for large IDs and amounts.
Worked examples
A webhook event
Input:
{"event_id": "evt_1Q9", "api_version": "2026-01", "created": 1764310000,
"amount": 2500000000, "livemode": false, "html_url": "https://example.com/e/1",
"data": {"customer_id": "cus_7", "ip": "203.0.113.9", "metadata": {}},
"retries": [{"at": "10:01", "status": 500}, {"at": "10:05", "status": 200, "latency_ms": 42}]}Generated output (the empty Metadata struct is left out here):
type Root struct {
EventID string `json:"event_id"`
APIVersion string `json:"api_version"`
Created int `json:"created"`
Amount int64 `json:"amount"`
Livemode bool `json:"livemode"`
HTMLURL string `json:"html_url"`
Data Data `json:"data"`
Retries []Retry `json:"retries"`
}
type Retry struct {
At string `json:"at"`
Status int `json:"status"`
LatencyMs *int `json:"latency_ms,omitempty"`
}
type Data struct {
CustomerID string `json:"customer_id"`
IP string `json:"ip"`
Metadata Metadata `json:"metadata"`
}2500000000 is above the 32-bit limit, so Amount is int64. latency_ms appeared in one retry only, so it is a pointer with omitempty. The array retries gave a singular struct name, Retry.
The empty-object case
Because "metadata": {} had no keys, the generator produced type Metadata struct {} with no fields, which would silently discard any metadata the API sends later. Replace the field’s type with map[string]any (or map[string]string if values are always text):
Metadata map[string]any `json:"metadata"`Pro tips
- The output has no package clause — add package models (or your package name) as the first line.
- interface{} and any are the same type since Go 1.18; feel free to search-and-replace for readability.
- For a stream of objects or a large file, use json.NewDecoder(r).Decode(&v) instead of reading everything into memory for json.Unmarshal.
Glossary
- Struct tag
- A string literal after a field that libraries read via reflection, such as json:"event_id".
- omitempty
- A tag option telling encoding/json to skip a field with a zero value when marshalling.
- Zero value
- The default value of a Go type: 0, "", false, or nil for pointers, slices and maps.
- Initialism
- An abbreviation written in one case in Go names, such as ID, URL or HTTP.
Frequently asked questions
Why are some fields pointers?
Pointers distinguish null or missing values from Go zero values like 0 and "".
What does omitempty do?
It leaves the field out when marshalling if it has the zero value.
Can I use int64 for IDs?
Yes — large integers in the sample automatically get int64.
Why is it EventID and not EventId?
Go style keeps initialisms in consistent case, and the generator applies that to a list of common ones like ID, URL, API, HTTP, IP and UUID.
Why did an empty object create an empty struct?
With no keys in the sample there is nothing to infer. Use map[string]any for free-form objects.
Can I use these structs with YAML or databases?
Yes, add more tags side by side, for example yaml:"event_id" or db:"event_id", inside the same back-quoted tag string.
Last updated 2026-11-28 · Built by Vinayak Agarwal · Part of Developer & Web (77 tools)