Skip to content
Sealpaste

Delete all recent work?

This removes the 0 items saved in this browser. Your settings stay.

Stop saving recent work?

New work won't be saved in this browser. The 0 items already saved can stay or go.

JSON to Go Struct

JSON

Paste JSON, or

CtrlV paste · Ctrl↵ run now

Go
Ready
Ln 1, Col 1
Runs in your browser. Nothing you paste is uploaded unless you share it.

Turn a JSON sample into Go structs with json tags, Go-style names (UserID, AvatarURL), int64 for big ids and omitempty

Paste an API response and get Go structs with json tags, ready for encoding/json. Python, Java and C# are one click away.

How to use it

  1. Paste a JSON sample, or open a .json file.
  2. Set the root type’s name; turn on omitempty to add it to fields that are missing or null in the sample.
  3. Copy the structs or download types.go. Choose another language above the editor for Python, Java or C#.

Examples

Go names and tags

Input
{"user_id": 42, "avatar_url": "https://example.com/a.png", "roles": ["admin"], "manager": null}
Result
type Root struct {
	UserID    int      `json:"user_id"`
	AvatarURL string   `json:"avatar_url"`
	Roles     []string `json:"roles"`
	Manager   any      `json:"manager"`
}

Field names follow Go’s style (ID and URL in capitals); the tag keeps the JSON key.

Optional fields with omitempty

Input
{"users": [{"id": 1, "name": "Ada"}, {"id": 2, "email": "bob@example.com"}]}
Result
type Root struct {
	Users []User `json:"users"`
}

type User struct {
	ID    int    `json:"id"`
	Name  string `json:"name,omitempty"`
	Email string `json:"email,omitempty"`
}

Name and Email are missing from one user each, so with omitempty on they get it.

A 64-bit id

Input
{"id": 9007199254740993, "score": 9.5, "nickname": null}
Result
type Root struct {
	ID       int64   `json:"id"`
	Score    float64 `json:"score"`
	Nickname any     `json:"nickname"`
}

The id is past 32 bits, so it is int64, and its digits are read exactly: JavaScript would round it to …992.

How the types are read

The sample is read with a few rules, the same for every language:

  • Every object gets its own named type; the name comes from its key (customer → Customer, line_items → LineItem).
  • The objects in one array are merged into one type, so a field missing from some of them is marked optional.
  • A field that is null in one place and a string in another is a nullable string. A field that is only ever null, or mixes kinds such as 1 and "a", gets the language’s “anything” type.
  • Integers and decimals are told apart by how they are written: 25 is an integer, 25.0 and 12.5 are not. An integer past 2,147,483,647 gets a 64-bit type, and one past 64 bits a big-number type, read from the digits, never rounded.
  • Empty arrays get a list of “anything”, since the sample doesn’t say what they hold.

Go specifics

The structs follow gofmt and Go’s naming style:

  • Names are written in Go style: user_id becomes UserID, avatar_url AvatarURL, and initialisms such as ID, URL, HTTP, JSON and API are in capitals.
  • Columns are lined up as gofmt does, with a tab indent, so the code is ready to commit.
  • A field that is null in some objects is a pointer (*string), so you can tell null from "".
  • Integers get int, or int64 past 32 bits; an integer wider than 64 bits gets json.Number, which keeps every digit (the import is added).
  • Nested objects get their own named struct instead of an inline one, so each can be reused and tested.

Make the sample count

The types are only as good as the sample: use a real response where optional fields are filled in, and include several array items when they differ.

Questions

What does omitempty do?
With omitempty, json.Marshal leaves the field out when it holds its zero value ("", 0, false, nil). It changes only what your program writes; reading JSON is the same either way.
Why is a null field typed as any?
A field that is only ever null in the sample says nothing about its type. Paste a sample where it has a value, or change any to the type you expect.
Can I get Python, Java or C# instead?
Yes: choose the language above the editor. Each has its own page: JSON to Python , JSON to Java and JSON to C#.
Is my JSON uploaded?
No. Everything runs in your browser, so your JSON stays on your device. Nothing you paste is uploaded unless you share a result, which is locked with your password first.

Related tools