{"record":{"id":"d915cf8537bf0519","repo":"jmoiron/sqlx","slug":"incompatible-type-for-jsontext","errorCode":null,"errorMessage":"Incompatible type for JSONText","messagePattern":"Incompatible type for JSONText","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"types/types.go","lineNumber":106,"sourceCode":"}\n\n// Scan stores the src in *j.  No validation is done.\nfunc (j *JSONText) Scan(src interface{}) error {\n\tvar source []byte\n\tswitch t := src.(type) {\n\tcase string:\n\t\tsource = []byte(t)\n\tcase []byte:\n\t\tif len(t) == 0 {\n\t\t\tsource = emptyJSON\n\t\t} else {\n\t\t\tsource = t\n\t\t}\n\tcase nil:\n\t\t*j = emptyJSON\n\tdefault:\n\t\t//lint:ignore ST1005 changing this could break consumers of this package\n\t\treturn errors.New(\"Incompatible type for JSONText\")\n\t}\n\t*j = append((*j)[0:0], source...)\n\treturn nil\n}\n\n// Unmarshal unmarshal's the json in j to v, as in json.Unmarshal.\nfunc (j *JSONText) Unmarshal(v interface{}) error {\n\tif len(*j) == 0 {\n\t\t*j = emptyJSON\n\t}\n\treturn json.Unmarshal([]byte(*j), v)\n}\n\n// String supports pretty printing for JSONText types.\nfunc (j JSONText) String() string {\n\treturn string(j)\n}\n","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/jmoiron/sqlx/blob/41dac167fdad5e3fd81d66cafba0951dc6823a30/types/types.go#L88-L124","documentation":"JSONText.Scan implements sql.Scanner and accepts string, []byte, and nil (SQL NULL maps to empty JSON). Any other driver type cannot be treated as JSON text, so Scan returns this error. The message is capitalized intentionally to preserve backwards compatibility.","triggerScenarios":"Scanning a column whose driver value is neither string, []byte, nor nil into a JSONText field — e.g. int64/float64 from a numeric column, time.Time from a timestamp column, or bool.","commonSituations":"JSONText used on an integer/timestamp column by mistake; a driver returning different types for TEXT/BLOB (some return string, others []byte, some custom types); ORM/driver version changes altering returned types.","solutions":["Verify the column actually stores JSON as text and the driver returns []byte/string.","Scan into []byte first and assign via j.Scan(raw) or append manually.","Use the correct sqlx type for the column's real type (Int64Text-like handling or plain types).","Upgrade/downgrade the driver or configure it to return strings for text columns."],"exampleFix":"// before\nvar j types.JSONText\nrow.Scan(&j) // src is time.Time\n// after\nvar raw []byte\nrow.Scan(&raw)\nj.Scan(raw)","handlingStrategy":"type-guard","validationCode":"func jsonTextScannable(src interface{}) bool {\n\tswitch src.(type) {\n\tcase []byte, string, nil:\n\t\treturn true\n\tdefault:\n\t\treturn false\n\t}\n}","typeGuard":"func assertJSONSource(v interface{}) ([]byte, bool) {\n\tswitch t := v.(type) {\n\tcase []byte:\n\t\treturn t, true\n\tcase string:\n\t\treturn []byte(t), true\n\t}\n\treturn nil, false\n}","tryCatchPattern":"var j types.JSONText\nif err := row.Scan(&j); err != nil {\n\tif err.Error() == \"Incompatible type for JSONText\" {\n\t\t// log reflect.TypeOf(src) from a plain interface{} scan\n\t}\n\treturn err\n}","preventionTips":["Use JSONText only on text/blob columns the driver returns as string/[]byte.","Check column DDL and driver type mapping during setup.","Treat NULL as valid (Scan maps nil to empty JSON) but non-text types as a bug.","Pre-scan into []byte when the driver's return type is uncertain."],"tags":["sql","scan","json","type-mismatch"],"backgroundTag":"scanner-incompatible-type","analyzedSha":"41dac167fdad5e3fd81d66cafba0951dc6823a30","analyzedAt":"2026-09-03T06:10:20.382Z","contentChangedAt":"2026-09-03T06:10:20.382Z","schemaVersion":2},"datasetVersion":"2026-09-10T12:17:11.382Z"}