JSON to Go Struct

Turn JSON into Go structs with exported field names, json tags, pointer types for nulls and optional omitempty. Handles nested objects and arrays. Free.

Nested objects become separate structs; nullable values use pointers; integers map to int64, decimals to float64, mixed or null-only values to any.

Runs entirely in your browser. Your data is never uploaded.

Related Tools

View All

What is JSON to Go Struct?

Generate idiomatic Go struct definitions from a JSON sample. Field names are exported PascalCase with Go initialisms (ID, URL, API…), each field carries a json:"…" tag with the original key, and nested objects become separate named structs.

How to use JSON to Go Struct

  1. Paste a JSON sample into JSON input (or click Load sample / Open file).
  2. Set the Root type name (default Root).
  3. Keep or clear Add omitempty to optional and nullable fields.
  4. Copy the generated Go code or Download it as a file.

Example

Input
{"id":1,"name":"A","tags":["x"]}
Output
type Root struct {
	ID   int64    `json:"id"`
	Name string   `json:"name"`
	Tags []string `json:"tags"`
}

When to use JSON to Go Struct

Use it to unmarshal API responses with encoding/json. It does not generate custom types for time values or enums; change string to time.Time yourself where appropriate.

How JSON to Go Struct works

The sample is walked recursively and every position collects the types it has seen. Arrays of objects are merged into one type: a key missing from some elements becomes optional, a key that is null somewhere becomes nullable, integers mixed with decimals become a floating-point type, and anything else that mixes types falls back to a dynamic type. Nested objects become their own named types (PascalCase of the key, array keys singularized: orders → Order), with numeric suffixes when names collide. Go output uses int64 for integers, float64 for decimals, string, bool, slices for arrays, a pointer (*T) for nullable values and any for dynamic or null-only values. Columns are aligned like gofmt output.

Common Use Cases

  • Model a REST response for json.Unmarshal
  • Create config structs from a JSON file
  • Speed up writing DTOs for a Go microservice
  • Compare field naming between JSON and Go code

FAQs

Why int64 instead of int?

int64 has a guaranteed size on every platform, which avoids overflow surprises with large IDs. Change it to int if you prefer.

When is a pointer used?

When the sample contains null for a key, the field becomes *T so that a missing value can be distinguished from the zero value.

What does omitempty do?

encoding/json skips the field when marshalling if it has its zero value. The option adds it only to fields that are optional or nullable in the sample.

Good next steps

Explore more categories