JSON to Go Struct Converter
CV-19Paste a JSON sample and press Convert to get Go structs with json tags. Field names are exported CamelCase, nested objects become their own structs, and null becomes a pointer.
Runs in your browser. Nothing is uploaded.
JSON
Go
How to use it
- Paste a JSON sample into the input box. Several array items show which keys are optional.
- Choose the root type name, such as Root or Response.
- Keep Go initialisms on if you want ID and URL instead of Id and Url.
- Press Convert and copy the structs into your .go file.
- Check the types against the real data, and rename fields where your code needs it.
How fields and types are chosen
Each key becomes an exported field in CamelCase, and the original key stays in the json tag, so user_id becomes UserID with the tag json:"user_id". Nested objects become structs named after their key, and arrays become slices such as []Order. A whole number is int, or int64 beyond the 32-bit range. A number with a decimal point or exponent is float64. Mixed types and empty arrays become interface{}.
Null, optional keys and pointers
A key that is null in the sample becomes a pointer, such as *string, so Go can tell null from an empty value. A null array stays a plain slice, since a nil slice already means no value. A key that is missing from some objects gets omitempty, and with the pointer option on it also becomes a pointer so that absent and zero differ. A key that is null in every item gives interface{}, because the sample shows no type.
Limits
The structs describe the sample, so a field that is always present in your sample but optional in real data is not marked optional. An integer too large for int64 is typed float64 and loses precision when decoded, so consider a string or json.Number for such IDs. Keys that are empty or contain a comma or backtick cannot be written in a struct tag and raise an error. Duplicate field names get a number added.
Error messages and what to do
| Message | What it means | Fix |
|---|---|---|
The key "a,b" cannot be written in a Go struct tag (it is empty or contains a comma or backtick). Rename it first. | A JSON key contains a comma or a backtick, or is empty. The struct tag format cannot hold these. | Rename the key in the sample, then write the field by hand with a custom decoder if you need that key. |
Expected a string key | The JSON has an object key without double quotes, or a comma before a closing brace. | Fix the JSON. The Validate action of the JSON Validator shows a hint for the cause. |
Questions people ask
How do I convert JSON to a Go struct?
Paste a JSON sample and press Convert. The tool writes a struct for the root object and one for each nested object, with json tags that match your keys. Copy the result into your Go file.
How does it decide between int and float64?
A number written without a decimal point or exponent is an integer: int if it fits in 32 bits, otherwise int64. Anything with a decimal point or exponent, such as 2.0 or 1e3, is float64. If an array mixes both, it is float64.
How are null values handled in Go?
A null in the sample gives a pointer, such as *string, so a missing value can be told from an empty string. If a key is null in every item, the type is interface{}, because the sample has no example of a real value.
Why are my field names capitalized?
Go only exports fields that start with a capital letter, and encoding/json can only fill exported fields. The tool capitalizes each word and keeps your original key in the json tag. Initialisms such as ID and URL are written in capitals when that option is on.
What does omitempty do?
It tells encoding/json to leave a field out when encoding if its value is empty, such as 0, an empty string or nil. The tool adds it to keys that are missing from some objects, or to every field if you turn that option on.
Does it work for large or nested JSON?
Yes, within reason. Nested objects, arrays of objects and arrays of arrays all work, and a few thousand items convert quickly. Very deep nesting is refused with a message. Review the result, since a sample cannot show every variation in real data.
About this tool
Infers types from your sample, so it only knows what the sample shows. Field names are exported CamelCase and the original key stays in the json tag. Whole numbers become int, or int64 beyond the 32-bit range, and anything with a decimal point or exponent becomes float64; an integer too big for int64 also becomes float64 and will lose precision. A key that is null in every item becomes interface{}, a key that is null in only some items becomes a pointer, and a key missing from some objects gets omitempty. Mixed types and empty arrays become interface{}. Keys with a comma or backtick cannot go in a tag and raise an error.