Skip to content
PithLab

↑↓ to move · Enter to open · Esc to close

JSON to Go Struct Generator

Turn a JSON sample into Go structs with json tags.

Use via API

Generate Go struct types with json tags from a sample JSON document. Integers and floats are kept apart; arrays of objects are merged into one struct. Send JSON, get JSON back.

POST https://pithlab.com/api/v1/tools/json-to-go

curl https://pithlab.com/api/v1/tools/json-to-go \
  -H "Authorization: Bearer $PITHLAB_KEY" \
  -H "Content-Type: application/json" \
  -d '{"input":"{\"id\":42,\"name\":\"Ada\",\"score\":9.5,\"tags\":[\"admin\"],\"address\":{\"city\":\"London\"}}","rootName":"User"}'

Fields and responseOpenAPI documentGet an API key

About the JSON to Go

Generate Go structs with json tags from a JSON sample. Go style names like ID and URL, int64 or float64 numbers, omitempty, pointers and inline or named nested structs.

How to use the JSON to Go

  1. Paste a JSON sample, such as an API response, into the input box.
  2. Set the root type name and choose whether nested objects become named structs or inline anonymous structs.
  3. Pick where to add omitempty and whether null values become pointers.
  4. Copy the Go code or download it as a .go file and add your package line.

Examples

Object with a nested object (default options)

Input

{"id": 7, "name": "Ada", "avatar_url": "https://example.com/a.png", "address": {"city": "London", "post_code": "N1"}}

Output

type Root struct {
	ID        int64   `json:"id"`
	Name      string  `json:"name"`
	AvatarURL string  `json:"avatar_url"`
	Address   Address `json:"address"`
}

type Address struct {
	City     string `json:"city"`
	PostCode string `json:"post_code"`
}
Nullable and missing keys with pointers and omitempty on optional fields

Input

{"users": [{"id": 1, "email": null}, {"id": 2, "email": "[email protected]", "admin": true}]}

Output

type Root struct {
	Users []User `json:"users"`
}

type User struct {
	ID    int64   `json:"id"`
	Email *string `json:"email,omitempty"`
	Admin bool    `json:"admin,omitempty"`
}
Array at the root with inline structs

Input

[{"sku": "A1", "price": 9, "qty": 2}, {"sku": "B2", "price": 4.5, "qty": 1}]

Output

type Root []struct {
	Sku   string  `json:"sku"`
	Price float64 `json:"price"`
	Qty   int64   `json:"qty"`
}

Frequently asked questions

How are Go field names chosen?

Each JSON key is split into words and written in PascalCase so the field is exported. Common initialisms stay upper case, so user_id becomes UserID and avatarUrl becomes AvatarURL. The json tag keeps the original key.

When is a number int64 and when is it float64?

Whole numbers like 42 become int64. If any sample for that field has a fraction or exponent, such as 4.5 or 2.0, the field becomes float64 so every value fits.

What does the pointer option do?

Fields that are null in the sample become pointers such as *string, so you can tell a missing value from an empty one. Slices, maps and any are left as they are because they can already be nil.

Why is a field typed as any?

The sample was only null, the array was empty, or the values had different types such as a number and a string. Add more realistic sample data, or change the type by hand.

Should I use inline or named structs?

Named structs are easier to reuse and to build in tests. Inline anonymous structs keep everything in one type, which suits a response you only decode once.