JSON to Go

Generate idiomatic Go structs with json tags from any JSON.

Runs in your browser Instant No sign-up
JSON input
Go

Paste, type or drop a file — output appears as you type.

Waiting for input
Processed locally · nothing is uploadedProcessed locally

How it works

Paste a JSON sample and get Go structs ready for encoding/json:

  1. Every object becomes a type Name struct, named after its property; the root is Root unless you rename it.
  2. Field names are exported PascalCase with Go initialisms (ID, URL, HTTP, JSON, UUID…), and every field keeps the original key in a json:"…" tag.
  3. Arrays of objects are merged into one struct, so fields that appear in only some items are still captured.
  4. Numbers are split into int64 and float64, booleans become bool, empty objects become map[string]any and empty arrays []any.
  5. Optional and nullable fields become pointers (see the FAQ); optional fields also get ,omitempty.
  6. Field names, types and tags are aligned in columns, as gofmt would format them.

Example

{
  "users": [
    { "id": 1, "nick": "a", "avatar": null },
    { "id": 2, "nick": null, "avatar": { "url": "x" } }
  ],
  "big": 9007199254740993
}

becomes

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

type User struct {
	ID     int64   `json:"id"`
	Nick   *string `json:"nick"`
	Avatar *Avatar `json:"avatar"`
}

type Avatar struct {
	URL string `json:"url"`
}

A root array produces a named slice type, e.g. type Root []int64.

Options

  • Root name — name of the top-level type.
  • Package — prepend a package clause so the output is a complete .go file.
  • omitempty on all — add ,omitempty to every tag, not just optional fields. Note that it also drops zero values such as 0, false and "" when marshaling.

Common mistakes

  • Generating from a single object. Every key it contains becomes a required, non-pointer field. Paste an array of several real responses so optional fields get pointers and ,omitempty.
  • Numbers that happen to be whole. A price of 25 in the sample becomes int64. If a later response sends 25.5, json.Unmarshal fails with a “cannot unmarshal number … into Go struct field” error. Include a decimal value in the sample, or change the type to float64 by hand.
  • Invalid sample JSON. The error panel shows the line and column and offers Fix with JSON Repair for comments, trailing commas and other near-JSON.

Decoding with the generated structs

var root Root
if err := json.Unmarshal(data, &root); err != nil {
	return fmt.Errorf("decode: %w", err)
}
for _, u := range root.Users {
	if u.Nick != nil { // nil when the value was null
		fmt.Println(*u.Nick)
	}
}

To reject payloads with unexpected keys, use a decoder with dec.DisallowUnknownFields().

Refining the output

  • Timestamps arrive as strings; change the field to time.Time if they are RFC 3339.
  • Swap int64 for int or uint32 where the range is known, and float64 for a decimal type for money.
  • Rename struct types if a name reads awkwardly. Element types are named with the singular of the array key (addresses → Address, categories → Category), which is a heuristic, not a dictionary.

Working in TypeScript or Python too? Generate matching types with JSON to TypeScript or JSON to Pydantic, or describe the payload language-neutrally with the JSON Schema Generator.

Frequently asked questions

When does a field become a pointer?

Two cases. A key that is missing from some objects in an array becomes a pointer with `,omitempty` (for example `*bool`), so you can tell "absent" from the zero value. A field whose sample values include null and one real type becomes a pointer too (`*string`, `*Avatar`). Slices and maps are never wrapped, because they are already nil-able.

Why is a field typed as `any`?

Either every sample value was null, or the field mixes incompatible types (for example a number in one object and a string in another). Replace `any` with the real type once you know it.

How are numbers typed?

Whole numbers become `int64`. If any sample value for the field has a decimal part, it becomes `float64`. Check fields like prices that happened to be whole in your sample.

Are Go naming conventions followed?

Yes. Keys in snake_case, camelCase or kebab-case are converted to exported PascalCase names, and common initialisms are upper-cased as the Go style guide recommends — `user_id` → `UserID`, `avatarUrl` → `AvatarURL`. The original key is always kept in the json tag.

Is my JSON uploaded?

No. The structs are generated entirely in your browser — nothing is uploaded.

Last updated