JSON to Go Converter
Go structs with encoding/json tags, generated from a JSON sample
Input
Output
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.
Every element of an array is inspected, not just the first, so a key that only some items have still makes it into the struct, with ,omitempty on its tag. Whole numbers are int, int64 or uint64 depending on the largest value, and integers beyond 64 bits become json.Number so no digits are lost. Nested arrays are typed properly ([][]int), mixed arrays become []any, and an empty object becomes map[string]any. If two different objects share a key name, the second struct is prefixed with its parent (BillingAddress) instead of silently reusing the wrong type.
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
Paste into the Input panel, Upload a .json or .txt file, or click Sample. Example input:
{"subscriberId":"SUB-1001","plan":"Unlimited 5G","dataUsage":45.2,"status":"active"}Struct Name names the root struct and Package sets the package clause. JSON Tags adds the struct tags. Use Pointers makes nested structs and nullable fields pointers, so a missing object decodes to nil instead of a zero value. If the input won't parse, check it with the JSON Validator.
Read the structs
The Output panel shows the file. For the sample you get Account, Device, Subscriber and RootObject, with Devices []Device and AddOns []string fields.
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?
A key that was only ever null becomes any, since the sample can't say more. A key that is null in one object and a string in another becomes string, or *string with pointers on.
What if the root is an array?
The objects inside it become the root struct and a comment tells you to decode into []RootObject. A root that is null, a string or an array of plain values gives a short explanation instead of code.
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. The structs are generated by JavaScript in this page and nothing is sent to a server. Input is capped at 256 KB.
Other JSON Tools You Might Need
Generating types for another service in a different language? These pages do the same job: