JSON to Go
Paste a JSON sample. Get Go types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.
An example
This JSON:
{
"id": 42,
"name": "Ada Lovelace",
"active": true,
"joined": "2020-01-02T03:04:05Z",
"tags": ["engineer", "writer"],
"address": { "city": "London", "postcode": null },
"scores": [9.5, 8, 10]
}
becomes this Go:
import "time"
type Root struct {
ID int64 `json:"id"`
Name string `json:"name"`
Active bool `json:"active"`
Joined time.Time `json:"joined"`
Tags []string `json:"tags"`
Address Address `json:"address"`
Scores []float64 `json:"scores"`
}
type Address struct {
City string `json:"city"`
Postcode interface{} `json:"postcode"`
}
With the reader and the writer
The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.
// Code generated from JSON Schema using quicktype. DO NOT EDIT.
// To parse and unparse this JSON data, add this code to your project and do:
//
// root, err := UnmarshalRoot(bytes)
// bytes, err = root.Marshal()
package main
import "time"
import "encoding/json"
func UnmarshalRoot(data []byte) (Root, error) {
var r Root
err := json.Unmarshal(data, &r)
return r, err
}
func (r *Root) Marshal() ([]byte, error) {
return json.Marshal(r)
}
type Root struct {
ID int64 `json:"id"`
Name string `json:"name"`
Active bool `json:"active"`
Joined time.Time `json:"joined"`
Tags []string `json:"tags"`
Address Address `json:"address"`
Scores []float64 `json:"scores"`
}
type Address struct {
City string `json:"city"`
Postcode interface{} `json:"postcode"`
}
What you need in your project
Nothing. `encoding/json` is in the standard library.
Every field gets a `json:"..."` tag, because Go matches a field by the tag and not by the name. Without the tag, a lower case key would not be found.
Turn on "Add omitempty" when you write the JSON back and want the empty fields to disappear. Be careful: `omitempty` also drops a false and a zero.
"Types only" gives the structs with no reader and no writer, and no package line. Use "Types and package only" when you want the package line kept.
The trap to know about
Go has no optional. A missing key and a key with the zero value look the same after reading. Use a pointer type when you must tell them apart.
The options you can change
These belong to Go, so the row of switches changes with the language.
| Option | What it does | Default |
|---|---|---|
| Types only | Plain types only | false |
| Types and package only | Plain types with package only | false |
| Package name | Generated package name | main |
| One file for each type | Renders each top-level object in its own Go file | false |
| Field tags | list of tags which should be generated for fields | json |
| Add omitempty | If set, all non-required objects will be tagged with ",omitempty" | false |
Questions
How do I turn JSON into Go?
Paste your JSON into the box, choose Go, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.
Does my JSON go to a server?
No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.
What do I need to add to my Go project?
Nothing. `encoding/json` is in the standard library.
How does it know which fields are optional?
From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.
Other languages
Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open JSON to Code