JSON to Go
Turn a JSON sample into Go struct types with json tags. Field names become exported Go names with common initialisms such as ID, URL and API in capitals, nested objects get their own structs, and the columns are aligned the way gofmt aligns them. Paste an API response and copy structs you can decode into with encoding/json.
Frequently asked questions
How are nested objects named?
Every nested object becomes its own Go type, named after the key that holds it in PascalCase, so address becomes Address. Objects inside arrays take the singular of the array key, so users becomes User. Objects with exactly the same fields share one type.
When is a field optional?
When an array holds several objects and a key is missing from some of them, that field is optional: the field becomes a pointer and its tag gets omitempty. A key present in every sample is required. A single object has nothing to compare with, so all of its keys are required.
How are null values handled?
A key that is null in some samples and has a value in others becomes a pointer, such as *string. A key that is only ever null tells the tool nothing about its real type, so it becomes any; paste a sample with a real value to get a precise type.
Which number types are used?
Whole numbers become int64 and numbers with a fraction become float64. A field that holds both becomes float64. Change int64 to int or another size by hand if your API guarantees a smaller range.
What happens to keys that are not valid names?
Every field name is rebuilt from the words in the key, so first-name becomes FirstName and user_id becomes UserID, and the json tag keeps the original key. Go needs exported names for encoding/json to fill a field, so every name starts with a capital letter.
Is my JSON uploaded or stored?
The JSON is sent to the server only to generate the code for this request and is not stored. The generator only reads the structure and the value types of your sample, so you can replace sensitive values with placeholders of the same type before you paste it.