What JSON to Go does
JSON to Go turns a sample JSON document into Go code: one struct with json tags per object, with a type for every field inferred from the value in the sample.
JSON to Go examples
The built-in sample
Input
{"name":"Ada Lovelace","active":true,"tags":["math","code"],"address":{"city":"London"}}Output
package main
type Address struct {
City string `json:"city"`
}
type Root struct {
Name string `json:"name"`
Active bool `json:"active"`
Tags []string `json:"tags"`
Address Address `json:"address"`
}This is the input that the Load sample button fills in.
Awkward keys, a null, a decimal and arrays
Input
{"order-id":7,"total":19.5,"paid":false,"coupon":null,"items":[{"sku":"A1","qty":2},{"sku":"B2","qty":1,"gift":true}],"tags":[]}Output
package main
type Items struct {
Sku string `json:"sku"`
Qty int `json:"qty"`
Gift bool `json:"gift"`
}
type Root struct {
OrderId int `json:"order-id"`
Total float64 `json:"total"`
Paid bool `json:"paid"`
Coupon interface{} `json:"coupon"`
Items []Items `json:"items"`
Tags []interface{} `json:"tags"`
}The sample has a key with a hyphen (order-id), a null (coupon), a decimal (total), an array of objects whose second item has an extra field, and an empty array.
Each output was produced by running this tool on the input above it.
How it works
The sample is walked recursively and each value is mapped to a Go type:
| Sample value | Go type |
|---|---|
| String | string |
| Whole number | int |
| Decimal number | float64 |
| true / false | bool |
| null, or an empty array’s items | interface{} |
| Array of T | []T |
| Object | a new named type |
Using the generated code
Each field carries a json tag with the original key, so decoding works even where the Go name differs:
var root Root
err := json.Unmarshal(data, &root)Details and limits
- Field names are made exportable by capitalising each word (
order-idbecomesOrderId). A key that starts with a digit keeps the digit and is not a valid Go identifier until you rename it. - The output starts with
package main; change it to your package name. - The fields are not column-aligned. Run
gofmton the file. - A null value becomes
interface{}. Use a pointer type such as*stringonce you know the real type. - A type is named after the key that holds the object:
itemsgives a type calledItems, with no attempt to make it singular. The top-level type is alwaysRoot. - If two different objects sit under the same key name in different places, two types with the same name are generated. Rename one of them.
- Objects in an array are merged into one type. A field that only some of them have is still generated as a required field.
- Input that is not valid JSON stops the tool, and the status line shows where the problem is. The JSON Validator page lists the usual causes.
Questions
Is my data uploaded when I use JSON to Go?
No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.
What does JSON to Go do?
JSON to Go turns a sample JSON document into Go code: one struct with json tags per object, with a type for every field inferred from the value in the sample.
Are there any limits I should know about?
Field names are made exportable by capitalising each word (order-id becomes OrderId). A key that starts with a digit keeps the digit and is not a valid Go identifier until you rename it. The output starts with package main; change it to your package name.
Is there a size limit?
There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.
Does it work on phones, and is it free?
Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.