JSON to Go Struct

Next

Paste a JSON sample and get Go struct definitions that parse it cleanly with encoding/json. Fields are exported (CamelCase), tagged with the original JSON keys, promoted to pointers when a value might be null, and nested objects get their own named struct declarations.

How to convert JSON to Go struct

  1. 1

    Paste the JSON

    A single sample is enough. Multiple samples improve nullable inference.

  2. 2

    Pick naming

    PascalCase for exported fields is the Go convention. Choose abbreviation style (`URL` vs `Url`, `ID` vs `Id`).

  3. 3

    Pick optional strategy

    Use pointers for nullable fields (most idiomatic), or `omitempty` tags.

  4. 4

    Copy the structs

    One root struct plus nested ones. Paste into a `.go` file and hit `go build`.

Example output

For:

{ "first_name": "Alice", "age": 30, "email": null, "tags": ["admin"] }

Output:

type User struct {
    FirstName string   `json:"first_name"`
    Age       int      `json:"age"`
    Email     *string  `json:"email"`
    Tags      []string `json:"tags"`
}

Type mapping

JSON value Go type
string string
integer int or int64
number (decimal) float64
boolean bool
null (with non-null sibling) *T (pointer)
array []T
object Named struct
mixed array []interface{}

Naming idioms the generator follows

  • Initialisms uppercased: id -> ID, url -> URL, api_key -> APIKey. This matches Go’s recommended style (and go vet / staticcheck rules).
  • Snake_case JSON -> PascalCase fields: first_name -> FirstName.
  • Single-word acronyms preserved: IP stays IP, HTTP stays HTTP.
  • JSON tag: json:"first_name" keeps the original key so encoding round-trips cleanly.

Optional fields: pointer or omitempty?

Strategy Use when
Pointer (*T) You need to distinguish “absent” from “zero value”
omitempty You only want to skip zero values on marshal; unmarshal stores zero for missing
Neither The field is always present and zero-values are meaningful

The generator defaults to pointer + omitempty for anything seen as null; tune based on your semantics.

Common mistakes

  • Using int where the JSON integer exceeds MaxInt32 on a 32-bit target. Use int64 for known-large values.
  • Marshaling back and losing key order. encoding/json emits keys in field-declaration order, not original JSON order. Sort manually if you need canonical output.
  • Mixed-shape arrays. []interface{} loses type safety. Consider union types with a type discriminator field instead.
  • Forgetting omitempty on optionals. Without it, optional fields serialize as "foo": null rather than being omitted.

Frequently Asked Questions

Use pointers when you need to distinguish “field absent” from “field is zero”. Use omitempty when the zero value is not a legal business value ("", 0, false always mean “skip this”). Many teams pick one and apply it consistently across a codebase.

Each nested object becomes a separate named struct declaration (e.g. User -> Address). Types are inlined only when the nested struct is tiny and used once; otherwise you get clean, reusable types.

Yes. id becomes ID, url becomes URL, api_key becomes APIKey. This is what go vet expects and what most Go style guides recommend.

The generator emits plain structs with standard json: tags. For custom UnmarshalJSON implementations (e.g. for dates in non-ISO formats), add them manually after generation.

Related Tools

Tool available in other languages