{"record":{"id":"3dd739f813bd14e5","repo":"elsa-workflows/elsa-core","slug":"failed-to-deserialize-stringvalue-to-underlyingtargettype","errorCode":null,"errorMessage":"Failed to deserialize {stringValue} to {underlyingTargetType}","messagePattern":"Failed to deserialize (.+?) to (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/clients/Elsa.Api.Client/Extensions/ObjectConverter.cs","lineNumber":83,"sourceCode":"        }\n\n        if (value is JsonNode jsonNode)\n            return jsonNode.Deserialize(targetType, options);\n\n        if (underlyingSourceType == typeof(string) && !underlyingTargetType.IsPrimitive && underlyingTargetType != typeof(object))\n        {\n            var stringValue = (string)value;\n\n            try\n            {\n                var firstChar = stringValue.TrimStart().FirstOrDefault();\n\n                if (firstChar is '{' or '[')\n                    return JsonSerializer.Deserialize(stringValue, underlyingTargetType, options);\n            }\n            catch (Exception)\n            {\n                throw new Exception($\"Failed to deserialize {stringValue} to {underlyingTargetType}\");\n            }\n        }\n\n        if (targetType == typeof(object))\n            return value;\n\n        if (underlyingTargetType.IsInstanceOfType(value))\n            return value;\n\n        if (underlyingSourceType == underlyingTargetType)\n            return value;\n\n        if (IsDateType(underlyingSourceType) && IsDateType(underlyingTargetType))\n            return ConvertAnyDateType(value, underlyingTargetType);\n\n        if (typeof(IDictionary<string, object>).IsAssignableFrom(underlyingSourceType) && underlyingTargetType.IsClass)\n        {\n            if (typeof(ExpandoObject) == underlyingTargetType)","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/clients/Elsa.Api.Client/Extensions/ObjectConverter.cs#L65-L101","documentation":"ObjectConverter.ConvertTo wraps JsonSerializer.Deserialize failures in a generic Exception with a message showing the string and target type. It is thrown when a JSON-looking string ('{' or '[' prefixed) cannot be deserialized to the underlying target type.","triggerScenarios":"Calling ConvertTo<T> (or via activity property conversion) with a string value that starts with '{' or '[' whose content is not valid JSON or does not match the target type shape.","commonSituations":"Workflow input/property values stored as JSON strings in definitions; malformed JSON saved by designers or hand-edited definitions; target type changed between versions.","solutions":["Validate the string is well-formed JSON of the expected shape before conversion","Fix the stored value so it matches the target type schema","Catch Exception around ConvertTo and log stringValue for diagnosis, then supply a fallback"],"exampleFix":"// before\nvar options = ObjectConverter.ConvertTo<MyOptions>(rawString);\n// after\nMyOptions options;\ntry { options = ObjectConverter.ConvertTo<MyOptions>(rawString); }\ncatch (Exception e) { logger.LogError(e, \"Bad value: {V}\", rawString); options = new MyOptions(); }","handlingStrategy":"try-catch","validationCode":"if (s.TrimStart().StartsWith('{') || s.TrimStart().StartsWith('['))\n    using (var doc = JsonDocument.Parse(s)) { /* shape check */ }","typeGuard":null,"tryCatchPattern":"try { result = ObjectConverter.ConvertTo<T>(s); } catch (Exception e) { logger.LogError(e, \"Convert failed for: {Value}\", s); result = default; }","preventionTips":["Validate stored JSON strings parse before conversion","Verify target type matches the JSON shape","Version-check workflow definitions after schema migrations"],"tags":["dotnet","json","deserialization"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"fe9217bdfa0e27f0e09e45006eb6898f616e513d","analyzedAt":"2026-09-13T20:32:34.702Z","contentChangedAt":"2026-09-13T20:32:34.702Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}