Code Generators
JSON to Go Converter
Paste a JSON or XML sample — the format is auto-detected — and get typed Go models with names, nesting and nullability inferred for you.
In short
Convert JSON to Go online: paste a sample and generate typed Go models with serialization helpers — powered by quicktype, in your browser.
By NaveenKumar T · Updated
- JSON or XML input, auto-detected
- Nested objects become types
- Nullability inferred
Runs entirely in your browser — nothing you paste or open here is uploaded, logged, or stored.How we handle data →
Example
Example: JSON order payload to Go
A small order document with a nested object, an array of two items, a decimal total and a null field — enough to show how each Go type, list and nullable is inferred. The same page accepts the XML form of this order too, so the sample below is an example rather than the only accepted format. The right-hand side is the exact output of this tool.
{
"id": "ord_9F2KX",
"status": "processing",
"paid": true,
"customer": { "name": "Ada Lovelace", "email": "ada@example.com", "phone": null },
"items": [
{ "sku": "KB-75", "qty": 1, "price": 149.99 },
{ "sku": "MS-12", "qty": 2, "price": 39.5 }
],
"total": 228.99
}// Code generated from JSON Schema using quicktype. DO NOT EDIT.
// To parse and unparse this JSON data, add this code to your project and do:
//
// root, err := UnmarshalRoot(bytes)
// bytes, err = root.Marshal()
package main
import "encoding/json"
func UnmarshalRoot(data []byte) (Root, error) {
var r Root
err := json.Unmarshal(data, &r)
return r, err
}
func (r *Root) Marshal() ([]byte, error) {
return json.Marshal(r)
}
type Root struct {
ID string `json:"id"`
Status string `json:"status"`
Paid bool `json:"paid"`
Customer Customer `json:"customer"`
Items []Item `json:"items"`
Total float64 `json:"total"`
}
type Customer struct {
Name string `json:"name"`
Email string `json:"email"`
Phone interface{} `json:"phone"`
}
type Item struct {
Sku string `json:"sku"`
Qty int64 `json:"qty"`
Price float64 `json:"price"`
}
Look for the "phone" field: sampled as null, it is typed as interface{}. "items" became []Item, and "total" is float64.
Learn more
From JSON sample to Go types
Hand-writing model classes from a JSON payload is transcription work: read a field, type a property, repeat eighty times, miss one. This page runs quicktype (the same engine behind the best-known generators) against your document, merging array elements to find the common shape and naming nested types from their keys.
Every number is float64, and the sample's 149.99 hides the consequence: an integer ID above 2^53 — 9,007,199,254,740,993, say — cannot be represented exactly and comes back rounded. If the API sends 64-bit integer IDs, change those fields to int64 (or json.Number) before the first production payload. Key matching on unmarshal is case-insensitive but the json tag is exact on marshal, so trust the tags over the field names.
How JSON values become Go types
Inference works from the values in your sample. Strings map to string, integers and decimals are told apart (228.99 becomes float64, while 1 and 2 stay integral), true/false become the language's boolean, and an object such as "customer" becomes its own named type. The "items" array is typed as []Item because every element shares the same keys — quicktype unifies the shapes of all array elements into one Item type.
Nulls are the one place inference needs your help: "phone": null carries no type information, so the field is emitted as interface{}. If the real API sometimes sends a string there, include a sample where the field is populated (paste several documents merged into one array) and the generated type becomes the union of what it saw.
Pasting XML works here too — a sample starting with "<" is parsed before inference runs — but if XML is your usual input, XML to Go is the page written for it: same generator, with the element-versus-attribute typing rules and a worked XML example.
Annotations, nulls and nested arrays in the generated Go
Go gets struct tags rather than annotations: every field ends with a json tag naming the exact key, so order_id can become the exported field OrderID without breaking encoding/json. Fields that were missing from one of several merged samples also pick up omitempty, so they are dropped on marshal instead of being written as null; a field that was present but null keeps its tag without omitempty. There are no xml tags anywhere — the generated structs describe JSON, which matters if you produced them from an XML sample.
A field whose only sampled value is null becomes interface{}, a box you have to type-assert before using. Give quicktype a second document where the field carries a value and it emits a pointer instead — *string, *int64 — so nil means absent and the zero value stays distinguishable from a real empty string or a real 0. Prefer zero values? Drop the star; the field set and the tags are the part worth generating. An empty array in the sample falls back to []interface{}.
Nested objects become their own named structs — Customer, Item — embedded by value, so root.Customer.Email works with no dereference and no allocation. Arrays of objects unify into []Item after quicktype merges every element's keys. An object that exists only to wrap a list produces two structs, an Items holding an []Item field, which is why XML samples nest a level deeper than you expect. Single-element arrays type fine but prove nothing about optional fields, and an empty array yields []interface{} with no element struct.
Help
Frequently asked questions
Everything you need to know about the JSON to Go.
01How do I generate Go classes from JSON?
Paste a representative document — JSON or XML, the format is detected automatically — and press Convert. A real API response works best. Nested objects become their own Go types, arrays become []Item, and the result downloads as Root.go.
02What does the generated Go code include?
Go generation emits structs with json tags exactly as encoding/json expects — nested types named sensibly, omitempty where a key was missing from some samples — the unmarshal target you'd otherwise write by hand.
03How do I use the generated Go code in my project?
Paste the file into a package (rename package main as needed). root, err := UnmarshalRoot(bytes) decodes and bytes, err := root.Marshal() encodes; the structs carry json tags with the exact wire names, so encoding/json round-trips the payload with no further configuration.
04How are null and missing JSON values represented in Go?
A field seen as both null and a value becomes a pointer type (*string, *int64), making absence representable — the standard Go pattern. A field that was only ever null has no type to infer, so it comes back as interface{}. If you prefer zero-values, drop the pointers; the field set is the part worth generating.
Keep working