Skip to main content
SnipKit

JSON to Golang Struct: Generate Go Types from Any JSON in Seconds

Aleksei Bobrik5 min read

The fastest way to convert JSON to Golang struct code is to paste the JSON into a generator. You get back a ready struct with json tags, nested types, and slices already mapped out. Hand-writing nested structs for a real API response — five levels deep, inconsistent casing — is slow and invites typos that only surface at runtime. SnipKit's JSON to Go Struct Converter exists to skip that step — free and fully client-side.

The guide below walks through a 3-step workflow for turning JSON into a working Go struct. After that, you'll see what the generator decided — type mapping, field naming, tag syntax — and how to fix the few things no tool can infer, like nullable fields.

How to Convert JSON to a Go Struct

  1. Validate and tidy the JSON first. A malformed API response (trailing comma, unescaped quote) will produce a broken struct or no output at all. Run it through JSON Validator to catch syntax errors, then JSON Formatter to indent it for easier reading before conversion.
  2. Paste into the JSON to Go Struct Converter. JSON to Go Struct Converter parses the payload and generates idiomatic Go structs with json tags, named nested types for nested objects, and time.Time inference for ISO-8601 date strings.
  3. Copy the struct into your project and unmarshal. Paste the generated type above your handler code, then call json.Unmarshal to populate it.

Given this JSON:

{
  "id": 42,
  "name": "Ada Lovelace",
  "created_at": "2026-08-06T10:00:00Z",
  "address": {
    "city": "London",
    "zip": "EC1A"
  }
}

the generator produces:

type User struct {
    ID        int       `json:"id"`
    Name      string    `json:"name"`
    CreatedAt time.Time `json:"created_at"`
    Address   struct {
        City string `json:"city"`
        Zip  string `json:"zip"`
    } `json:"address"`
}
var u User
if err := json.Unmarshal(data, &u); err != nil {
    log.Fatal(err)
}

What the Generator Decides for You

Type mapping is what the JSON to Go Struct Converter handles automatically. Go's encoding/json package decodes JSON numbers to float64 by default when no concrete type is given (pkg.go.dev/encoding/json). The generator instead inspects the sample value, so a whole number like 42 becomes int rather than a blanket float64. The converter also maps ISO-8601 date strings to time.Time, nested JSON objects to named nested structs, and JSON arrays to Go slices. Element types for slices come from the first array item.

Go Struct Tags Explained

A Go struct tag like `json:"field_name"` tells encoding/json how to map a JSON key to a Go field. Tags bridge the gap between CamelCase Go fields (CreatedAt) and snake_case JSON keys (created_at). Two options change behavior. omitempty drops a field from the output when it holds a zero value; "-" skips a field entirely — useful for a password hash that should never serialize.

Only exported fields, those starting with a capital letter, participate in marshaling at all; lowercase fields stay invisible to encoding/json regardless of tags (pkg.go.dev/encoding/json). Struct tags are a language-level feature, defined in the Go spec as string literals attached to struct fields (go.dev/ref/spec#Struct_types).

Fixing What the Generator Can't Know

A JSON to Go Struct Converter can't tell whether a field is sometimes absent, sometimes null, or always present — it only sees one sample payload. Two fixes cover most gaps:

  • Pointers for optional fields. Change string to *string (or *int, *bool) on any field that might be missing or null. A pointer lets you tell "field absent" (nil) apart from "field present but zero value" ("" or 0).
  • Maps for dynamic keys. If an object's keys vary per response, replace the generated nested struct with map[string]any instead of enumerating every possible key.

One sample payload rarely covers every real-world shape. Generate varied test payloads with Mock Data Generator — including missing fields and nulls — and re-run them through your struct to confirm the fixes hold.

FAQ

Why do JSON numbers become float64 in Go? Go's encoding/json package defaults untyped JSON numbers to float64 because JSON has no native distinction between integers and floats (pkg.go.dev/encoding/json). A generated Go struct sidesteps this by inferring int for whole-number sample values, so float64 only shows up when the source data actually has decimals.

Why are my struct fields empty after json.Unmarshal? Unexported (lowercase) struct fields are silently ignored by encoding/json, so a field named name instead of Name never gets populated. Check that every field starts with a capital letter and that its json tag matches the source key exactly.

How do I handle null or missing JSON fields? Use a pointer type (*string, *int, *bool) for any field that can be null or absent in the source JSON. A nil pointer clearly signals "not provided" rather than colliding with a legitimate zero value like 0 or "". Add omitempty to the tag if you also want the field excluded from output when it's empty.

Conclusion

Converting JSON to a Golang struct comes down to three moves: validate the JSON, generate the struct, then adjust pointers and tags for nulls and dynamic keys. Paste your next API response into the JSON to Go Struct Converter and get a compilable struct back in seconds instead of hand-typing nested types.

Cover photo via Unsplash

Related Articles