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).
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 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
- Change the field type or use
json:"age,string" - Use
json.RawMessageorinterface{}for variable shapes - Inspect the raw payload first
Detailed fix by platform
Go
type User struct { Age intjson:"age,string"}accepts"age":"30"
How to diagnose
- Field — Named in message?
- JSON — Actual type?
- 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:
Similar errors
- assignment mismatch: 1 variable but f returns 2 values Go: assignment mismatch: 1 variable but os.Open returns 2 values
- cannot assign to struct field m[k].f in map Go: cannot assign to struct field users["bob"].Age in map
- cannot refer to unexported name Go: cannot refer to unexported name pkg.helper (undefined: pkg.helper)
Most viewed in Go
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 4 Nov 2026