Developer / JSON
JSON to Go Struct
Paste a JSON sample and get Go struct definitions with exported field names, the initialisms Go's linters expect, and json tags, ready to paste into your code.
JSON to Go Struct: The converter reads the JSON in your browser and writes a struct for each object. Keys become exported CamelCase names with initialisms such as ID, URL, and HTTP in capitals, as golint recommends, and each field keeps its original key in a json tag, laid out as gofmt would format it. Objects in an array are merged, so a key missing from some of them gets omitempty and a key that is sometimes null becomes a pointer. Whole numbers become int64 unless a decimal appears in the same place, which makes it float64. Runs 100% locally in your browser with zero server file uploads.
- Category
- Developer tools
- Runs
- In your browser
- Cost
- Free · no sign-up
- Availability
- Ready to use
Runs entirely in your browser
Go
type User struct {
ID int64 `json:"id"`
UserName string `json:"user_name"`
AvatarURL string `json:"avatar_url"`
Score float64 `json:"score"`
Tags []string `json:"tags"`
Address Address `json:"address"`
}
type Address struct {
City string `json:"city"`
ZipCode string `json:"zip_code"`
}Keys missing from some objects in an array get omitempty; keys that are sometimes null become pointers. Check the types against your API's documentation: a sample only shows what it contains.
From a sample to a type
Go decodes JSON into structs with encoding/json, which matches each key to a field by its json tag, or by name ignoring case when there is no tag. Only exported fields, those starting with a capital letter, are filled in, which is why every field here is capitalised and keeps the original key in its tag.
A sample only shows what it contains. A list of users where one lacks an email tells the converter that email is optional; a single object cannot. Paste several representative objects in an array when you have them, and check the result against the API's documentation. The JSON formatter helps spot the structure of a long response first.
Other languages and schemas
The same sample can become TypeScript interfaces with the JSON to TypeScript converter, for a front end that reads the same API, or a JSON Schema to validate payloads at the boundary.
How to use it
- Paste a JSON object or array, such as an API response.
- Name the top-level type and choose separate or nested structs.
- Copy the Go code.
Privacy & limitations
Your JSON is converted in your browser and never uploaded.
Related tools
Frequently asked questions
Why is user_id called UserID and not UserId?
Go style writes initialisms in a single case, so UserID, APIKey, and AvatarURL. The converter uses the list of initialisms from golint, the linter that popularised the rule.
When does a field get omitempty or a pointer?
When the sample is an array of objects and a key is missing from some of them, the field gets omitempty, so marshalling leaves it out when empty. When a key is null in some objects, the field becomes a pointer, so nil can stand for null. You can also add omitempty to every field.
Why int64 and float64?
JSON has a single number type. A value written without a decimal point becomes int64, and anything with a fraction becomes float64. If an ID could exceed 2^53 or is really a string in disguise, change the type by hand: a sample only shows the values it contains.
What happens to null or empty arrays?
With nothing to go on, a field that is always null, or an empty array, gets the type any (interface{}), which accepts anything. Replace it with the real type when you know it.
Free tool · runs in your browser · no account required