Skip to content
Devtoolry

JSON to Go

Convert JSON to Go structs online with json tags, pointers for null values and omitempty for optional keys. Paste an API response and copy the types.

Something not working, or missing a feature? Send feedback

About the JSON to Go

The JSON to Go converter turns a sample API response into Go structs with json tags, so you can decode it with encoding/json straight away. Field names follow Go conventions (ID, URL, APIKey), nested objects become their own structs, and keys that are missing from some array items get omitempty.

Conversion happens in your browser, so your files and records stay private.

How to use the JSON to Go

  1. Paste a JSON sample, ideally a real response with every field filled in.
  2. Set the name of the root struct.
  3. Copy the structs into your package and decode with json.Unmarshal.

Example

Go structs from a user with projects

Input

{
  "id": 7,
  "fullName": "Grace Hopper",
  "email": null,
  "roles": ["admin"],
  "team": { "name": "Compilers", "size": 4 },
  "projects": [
    { "name": "COBOL", "year": 1959 },
    { "name": "A-0", "year": 1952, "archived": true }
  ]
}

Result

type User struct {
	ID       int64     `json:"id"`
	FullName string    `json:"fullName"`
	Email    any       `json:"email"`
	Roles    []string  `json:"roles"`
	Team     Team      `json:"team"`
	Projects []Project `json:"projects"`
}

type Project struct {
	Name     string `json:"name"`
	Year     int64  `json:"year"`
	Archived bool   `json:"archived,omitempty"`
}

type Team struct {
	Name string `json:"name"`
	Size int64  `json:"size"`
}

Keys missing from some array items (archived) get omitempty, and initialisms follow Go style (ID, not Id). email was only ever null, so its type is unknown (any): paste a sample where it has a value to get *string.

Common mistakes

  • Optional booleans and numbers. With omitempty, a missing archived and archived: false both decode to false. If the difference matters, change the field to *bool.
  • A sample with nulls. A field that is only ever null becomes any. Generate from a response where every field has a value, then add pointers for the ones that can be null.

Works well with the JSON to Go

Frequently asked questions

Why are some fields pointers?

A field that was null in the sample becomes a pointer (*string, *int64), so nil can represent null. Slices and maps can already be nil, so they stay as they are.

Why is a field typed as any?

Its value was always null, or it held different kinds of values (for example a number in one item and a string in another), so the converter cannot pick one type. Use a sample where the field has a real value.

Which integer and float types are used?

Whole numbers become int64 and numbers with a fraction become float64. If a field holds both, it becomes float64. Change them to int or float32 if you know the range.