Code generator

JSON to Go Struct

Paste a JSON example, such as an API response, and get Go structs with the right json tags, ready for encoding/json.

How to generate Go structs

  1. Paste a real JSON example, or press Sample.
  2. Set the root type name. Nested objects get their own structs, named after their keys.
  3. Copy the code or download types.go, then unmarshal with json.Unmarshal(data, &v).

How JSON maps to Go

JSONGo
stringstring
whole numberint64
decimal numberfloat64
true / falsebool
objecta named struct
array[]T
value that can be nullpointer, e.g. *string
mixed types, or only nullany

Field names and tags

Go only encodes and decodes exported fields, which start with a capital letter. So user_name becomes UserName, and a struct tag such as `json:"user_name"` keeps the original key. Common initialisms follow Go style: id becomes ID and url becomes URL.

When objects in an array have different keys, every key is included, and keys that are missing from some objects get omitempty so they are left out when you encode the struct again.

Frequently asked questions

Why are the field names capitalized?

encoding/json only reads and writes exported fields, and in Go a field is exported when its name starts with a capital letter. The json tag keeps the original key name.

Why is a field a pointer?

The value was null in the example. A pointer can be nil, so you can tell a missing or null value apart from an empty string or zero.

How are dates typed?

As string, because JSON has no date type. If the value is RFC 3339, you can change the type to time.Time, which encoding/json parses automatically.

What does any mean?

any is an alias for interface{} (Go 1.18+). It is used when a field had several different types, or only null, in the example.

Is my data uploaded?

No. Everything runs with JavaScript inside your browser tab. Nothing you paste is sent to a server or stored.