JSON to Go Converter
Go structs with encoding/json tags, generated from a JSON sample
Input
Output
Paste your JSON on the left and the Go code shows up here.
Or drop a file on this box, , or .
What does the JSON to Go converter generate?
Writing a struct for every level of a JSON response is the least fun part of calling an API from Go. Paste a sample here and you get a single file: one package clause, then a type ... struct for every object, nested types first. Field names follow Go style, including the usual initialisms, so subscriberId becomes SubscriberID and apiUrl becomes APIURL. Each field carries a json:"subscriberId" tag for encoding/json, and the columns are aligned the way gofmt would.
Each array element is read, so giftWrap, which only the second order line has, still gets a field, tagged json:"giftWrap,omitempty". Integers are typed by the largest value present (int, int64, uint64), and anything wider than 64 bits becomes json.Number so the digits survive. An array of arrays comes out as [][]int, an array of mixed values as []any, and {} as map[string]any. When a key name turns up in two different places, the second struct takes its parent's name as a prefix (BillingAddress), so one type never quietly stands in for another.
The output is checked with go vet and gofmt against the page Sample and a set of awkward inputs: keys with spaces and dashes, keys starting with digits, Go keywords and colliding names. It uses any, so it needs Go 1.18 or later. Everything runs in your browser.
How to use it
The structs update as you type; there's no Convert button.
Paste a JSON sample
Put JSON in the Input panel by pasting it or through Upload; Sample loads a subscriber with an account, devices and add-ons. An order payload shows initialisms, omitempty and a null voucher in one go:
{"orderId":"ORD-3310","apiUrl":"/v2/orders/3310","totalCents":4200,"shipTo":{"name":"Harriet Vane","zip":"OX1 2JD"},"lines":[{"sku":"SKU-101","qty":2},{"sku":"SKU-330","qty":1,"giftWrap":true}],"voucher":null} Struct Name and Package set the root type and the package clause, and JSON Tags switches the backtick tags on or off. Use Pointers turns nested structs into pointers (ShipTo *ShipTo) so an absent address decodes to nil, and a value that was null somewhere becomes *string or similar. A key that is merely missing from some items keeps its plain type: GiftWrap stays bool and reads false when absent.
Read the structs
For the order above the file declares ShipTo and Line before RootObject, whose fields read OrderID, APIURL, TotalCents int, ShipTo ShipTo, Lines []Line and Voucher any. That last one is any because the sample never showed a voucher that was not null.
Copy or download
Copy the code or Download a .go file, then json.Unmarshal(body, &root). The Go blog post JSON and Go covers how tags and decoding fit together.
Where this saves time
Calling third-party APIs
Paste a response from a payments or shipping API and get structs to unmarshal into, with the awkward keys (event-type, 2fa_enabled) already mapped through tags.
Webhook handlers
Paste a few sample events as an array. Fields that only appear in some events come out with omitempty, which tells you which ones your handler can't rely on.
Config files
Turn a JSON config into a typed struct and read it with json.NewDecoder(f).Decode(&cfg). Add DisallowUnknownFields if you want typos to fail loudly.
Large IDs
IDs like 8901240544102066246 get int64, and values beyond 64 bits get json.Number, so you notice the edge cases before production does.
Common Questions
Does the output compile?
Yes for the cases we test: the page Sample plus inputs with nested arrays, mixed arrays, nulls, reserved words, colliding names and big numbers all pass go vet. Review the types anyway; a sample can't tell whether a null field is really a string.
What do the Use Pointers and JSON Tags options change?
Use Pointers turns nested structs into *Account and nullable values into *string, so you can tell "missing" from "empty". JSON Tags adds the json:"..." tags; turn it off only if your keys already match the field names.
What type does a null value get?
When every sample has "voucher": null, the generator learns nothing about it, so the field is Voucher any with omitempty. Once one record shows a string, it becomes string, or *string with Use Pointers on, which is the only way to tell a null voucher from an empty one after json.Unmarshal.
What if the root is an array?
Its objects become the root struct, and the file opens with // The JSON root is an array: decode it into []Order. when Struct Name says Order. A root like 42 or ["eu-west-1","us-east-2"] has no fields to declare, so the output panel names what it found and stops there.
Which Go version do I need?
Go 1.18 or later, because the output uses any. On older versions replace it with interface{}.
Is my JSON uploaded?
No. Struct generation is a TypeScript module bundled into the page, so typing runs it locally and no request carries your JSON out. Keep the input to a few representative records; the page stops at 256 KB.
Other JSON Tools You Might Need
Generating types for another service in a different language? These pages do the same job: