{"record":{"id":"f9814ef34b97c2dd","repo":"ory/hydra","slug":"unable-to-decode-json-payload-into-t-w","errorCode":null,"errorMessage":"unable to decode JSON payload into %T: %w","messagePattern":"unable to decode JSON payload into %T: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"oryx/sqlxx/types.go","lineNumber":504,"sourceCode":"}\n\n// JSONScan is a generic helper for retrieving a SQL JSON-encoded value.\nfunc JSONScan(dst, value any) error {\n\t// Note: raw is a string (not []byte) because the MySQL driver reuses byte slices across scans.\n\t// Using strings avoids the need to manually copy the byte slice.\n\tvar raw string\n\tswitch v := value.(type) {\n\tcase nil:\n\t\traw = \"null\"\n\tcase string:\n\t\traw = v\n\tcase []byte:\n\t\traw = string(v)\n\tdefault:\n\t\treturn fmt.Errorf(\"unable to scan type %T as JSON into %T\", value, dst)\n\t}\n\tif err := json.Unmarshal([]byte(raw), dst); err != nil {\n\t\treturn fmt.Errorf(\"unable to decode JSON payload into %T: %w\", dst, err)\n\t}\n\treturn nil\n}\n\n// NullInt64 represents an int64 that may be null.\n// swagger:type int64\n// swagger:model nullInt64\ntype NullInt64 struct {\n\tInt   int64\n\tValid bool // Valid is true if Duration is not NULL\n}\n\n// Scan implements the Scanner interface.\nfunc (ns *NullInt64) Scan(value interface{}) error {\n\td := sql.NullInt64{}\n\tif err := d.Scan(value); err != nil {\n\t\treturn err\n\t}","sourceCodeStart":486,"sourceCodeEnd":522,"githubUrl":"https://github.com/ory/hydra/blob/4174065ffb052799890f7480f5360a877a67ffc1/oryx/sqlxx/types.go#L486-L522","documentation":"After JSONScan obtains the raw text (from null, string, or []byte) it calls json.Unmarshal into the destination. This error wraps that failure, meaning the raw value is present but is not valid JSON or does not match the destination's shape (unknown fields are ignored by default, so shape/type mismatch is the usual cause).","triggerScenarios":"json.Unmarshal fails because the column content is malformed JSON (truncated, plain text, HTML error page stored in the column) or the JSON structure does not fit the destination type (e.g. object scanned into a slice or primitive).","commonSituations":"Legacy rows written before a schema/type change, application code writing non-JSON strings into a JSON column, JSON arrays stored where a struct is expected, or BOM/whitespace-corrupted payloads.","solutions":["Print/inspect the offending raw value for the failing row and fix or migrate the stored data","Validate the payload matches the destination shape (json.Valid, or unmarshal into json.RawMessage/map first)","Use json.RawMessage or a flexible destination type if the column may hold heterogeneous JSON","Pre-validate data on write with json.Marshal/strict unmarshal so bad data never reaches the DB"],"exampleFix":"// before\ntype Config struct { Host string `json:\"host\"` }\nvar c Config\n// column value: `[\"a\",\"b\"]` -> unmarshal error\n// after\ntype Config struct { Hosts []string `json:\"hosts\"` }\nvar c Config\nif err := json.Unmarshal([]byte(raw), &c); err != nil {\n    return fmt.Errorf(\"unable to decode JSON payload into %T: %w\", dst, err)\n}","handlingStrategy":"validation","validationCode":"if !json.Valid([]byte(raw)) {\n    return fmt.Errorf(\"invalid JSON payload: %s\", raw)\n}","typeGuard":null,"tryCatchPattern":"var target MyType\nif err := json.Unmarshal(data, &target); err != nil {\n    var syn *json.SyntaxTypeError\n    if errors.As(err, &syn) {\n        log.Printf(\"bad JSON syntax at offset %d\", syn.Offset)\n    }\n    return fmt.Errorf(\"unable to decode JSON payload into %T: %w\", &target, err)\n}","preventionTips":["Validate data with json.Valid before storing in DB columns","Use json.RawMessage destinations for heterogeneous columns","Write via json.Marshal so stored values are always valid JSON"],"tags":["json","unmarshal","database","decoding"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"4174065ffb052799890f7480f5360a877a67ffc1","analyzedAt":"2026-09-03T14:52:41.581Z","contentChangedAt":"2026-09-03T14:52:41.581Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}