json: cannot unmarshal string into Go struct field Go Origin: Application & API Last updated 4 Nov 2026

Go: json: cannot unmarshal string into Go struct field User.age of type int

The JSON value's type does not match the Go field type (string vs number, object vs array).

Exact message you may see json: cannot unmarshal string into Go struct field User.age of type intjson: cannot unmarshal object into Go value of typejson: cannot unmarshal array into Go struct fieldjson: cannot unmarshal number into Go value of type stringjson: cannot unmarshal number 1.5 into Go struct field
Seen on: Go

Meaning

The error names the field and both types. Fix the struct (use string, json.Number, or a custom type), fix the data source, or add the ,string tag option when numbers arrive as quoted strings. Since Go 1.20 decoding continues past this error for other fields but returns the first error.

Common causes

  • API returns "30" (string) but field is int
  • Object returned where array expected
  • null into non-pointer scalar
  • Field name/tag mismatch leaving zero values
  • Unexported fields never populated

⚡ Quick fix

  1. Change the field type or use json:"age,string"
  2. Use json.RawMessage or interface{} for variable shapes
  3. Inspect the raw payload first

Detailed fix by platform

Go

  1. type User struct { Age int json:"age,string" } accepts "age":"30"

How to diagnose

  1. Field — Named in message?
  2. JSON — Actual type?
  3. Tag — Matches key?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

assignment mismatch: 1 variable but f returns 2 values — Go: assignment mismatch: 1 variable but os.Open returns 2 values (Go)assignment mismatch: …cannot assign to struct field m[k].f in map — Go: cannot assign to struct field users["bob"].Age in map (Go)cannot assign to stru…cannot refer to unexported name — Go: cannot refer to unexported name pkg.helper (undefined: pkg.helper) (Go)cannot refer to unexp…invalid character looking for beginning of value — Go: invalid character '<' looking for beginning of value (Go)invalid character loo…listen tcp :8080: bind: address already in use — Go: listen tcp :8080: bind: address already in use (Go)listen tcp :8080: bin…panic: assignment to entry in nil map — Go: panic: assignment to entry in nil map (Go)panic: assignment to …panic: interface conversion — Go: panic: interface conversion: interface {} is string, not int (Go)panic: interface conv…reflect: call of reflect.Value.X on zero Value — Go: panic: reflect: call of reflect.Value.Elem on zero Value (Go)reflect: call of refl…json: can…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.