{"record":{"id":"bfca487157279244","repo":"gofr-dev/gofr","slug":"failed-to-unmarshal-json-w","errorCode":null,"errorMessage":"failed to unmarshal JSON: %w","messagePattern":"failed to unmarshal JSON: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/gofr/datasource/kv-store/dynamodb/dynamo.go","lineNumber":135,"sourceCode":"\tif tracer, ok := tracer.(trace.Tracer); ok {\n\t\tc.tracer = tracer\n\t}\n}\n\n// ToJSON converts a Go struct to JSON string for use with KVStore.Set.\nfunc ToJSON(value any) (string, error) {\n\tjsonData, err := json.Marshal(value)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to marshal value to JSON: %w\", err)\n\t}\n\n\treturn string(jsonData), nil\n}\n\n// FromJSON converts a JSON string to a Go struct for use with KVStore.Get.\nfunc FromJSON(jsonData string, dest any) error {\n\tif err := json.Unmarshal([]byte(jsonData), dest); err != nil {\n\t\treturn fmt.Errorf(\"failed to unmarshal JSON: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc (c *Client) Get(ctx context.Context, key string) (string, error) {\n\tif !c.connected {\n\t\treturn \"\", errClientNotConnected\n\t}\n\n\tspan := c.addTrace(ctx, \"get\", key)\n\tdefer c.sendOperationsStats(time.Now(), \"GET\", \"get\", span, key)\n\n\tinput := &dynamodb.GetItemInput{\n\t\tTableName: aws.String(c.configs.Table),\n\t\tKey: map[string]types.AttributeValue{\n\t\t\tc.configs.PartitionKeyName: &types.AttributeValueMemberS{Value: key},\n\t\t},","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/datasource/kv-store/dynamodb/dynamo.go#L117-L153","documentation":"FromJSON decodes a JSON string into dest for use with KVStore.Get. This error wraps json.Unmarshal failures with 'failed to unmarshal JSON: %w' when the stored string is invalid JSON or incompatible with dest's shape.","triggerScenarios":"Calling FromJSON on a value that was Set as a raw string rather than via ToJSON; dest type not matching stored JSON (e.g. number into string field); empty or truncated stored value; malformed JSON written by another writer.","commonSituations":"Mixed writers to the same KV store (one uses ToJSON, one stores plain text), schema drift after struct field renames/type changes, reading keys written by an older app version.","solutions":["Always write with ToJSON so stored values are valid JSON","Pass a dest whose fields match the stored JSON types; check the wrapped err for a *json.UnmarshalTypeError detail","Handle legacy/plain-string values: attempt FromJSON and fall back to raw string handling","Validate the stored value is non-empty before unmarshaling"],"exampleFix":"// before\nvar u User\nerr := FromJSON(raw, &u) // fails on plain text\n// after\nvar u User\nif err := FromJSON(raw, &u); err != nil {\n    u = User{Name: raw} // fallback for legacy raw values\n}","handlingStrategy":"validation","validationCode":"func validJSON(s string) error {\n    if s == \"\" { return errors.New(\"empty JSON\") }\n    if !json.Valid([]byte(s)) { return errors.New(\"invalid JSON payload\") }\n    return nil\n}\n// call before FromJSON: if err := validJSON(raw); err != nil { handle }","typeGuard":"func canUnmarshal[T any](s string) bool { var v T; return json.Unmarshal([]byte(s), &v) == nil }","tryCatchPattern":"var dest MyStruct\nif err := dynamodb.FromJSON(raw, &dest); err != nil {\n    var te *json.UnmarshalTypeError\n    if errors.As(err, &te) { log.Printf(\"type mismatch at %v\", te.Field) }\n    return fallbackDecode(raw)\n}","preventionTips":["Write all values through ToJSON so the store holds valid JSON","Version your stored structs to survive schema drift","Reject/truncate-proof: never store empty strings for JSON values","Add round-trip tests (Test_FromJSONError style) for stored types"],"tags":["json","deserialization","dynamodb","kv-store"],"backgroundTag":"json-unmarshaling-failed","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}