{"record":{"id":"2d7e58ef02377272","repo":"elsa-workflows/elsa-core","slug":"failed-to-deserialize-stringvalue-to-underlyingtargettype-2d7e58","errorCode":null,"errorMessage":"Failed to deserialize {stringValue} to {underlyingTargetType}","messagePattern":"Failed to deserialize (.+?) to (.+?)","errorType":"exception","errorClass":"TypeConversionException","httpStatus":null,"severity":"error","filePath":"src/modules/Elsa.Expressions/Helpers/ObjectConverter.cs","lineNumber":170,"sourceCode":"        {\n            var stringValue = (string)value;\n\n            if (underlyingTargetType == typeof(byte[]))\n            {\n                // Byte arrays are serialized to base64, so in this case, we convert the string back to the requested target type of byte[].\n                return Convert.FromBase64String(stringValue);\n            }\n\n            try\n            {\n                var firstChar = stringValue.TrimStart().FirstOrDefault();\n\n                if (firstChar is '{' or '[')\n                    return JsonSerializer.Deserialize(stringValue, underlyingTargetType, serializerOptions);\n            }\n            catch (Exception e)\n            {\n                throw new TypeConversionException($\"Failed to deserialize {stringValue} to {underlyingTargetType}\", value, underlyingTargetType, e);\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        var internalSerializerOptions = InternalSerializerOptions;\n\n        if (typeof(IDictionary<string, object>).IsAssignableFrom(underlyingSourceType) && (underlyingTargetType.IsClass || underlyingTargetType.IsInterface))","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/modules/Elsa.Expressions/Helpers/ObjectConverter.cs#L152-L188","documentation":"ObjectConverter.ConvertTo attempts to JsonSerializer.Deserialize a string that starts with '{' or '[' into the target type. If deserialization fails for any reason it wraps the exception in a TypeConversionException reporting that it failed to deserialize the string value to the underlying target type.","triggerScenarios":"Converting a string starting with '{' or '[' (via ConvertTo/ObjectConverter) into a target type whose JSON does not match — e.g. passing a JSON array into a target expecting an object, malformed JSON, or a property name/type mismatch under the configured serializer options.","commonSituations":"Workflow inputs supplied as JSON text into object-typed variables, malformed JSON from clipboard/designer, or changing case-sensitivity/serializer options so previously matching payloads no longer deserialize.","solutions":["Validate that the string is well-formed JSON and its shape matches the target type before conversion","Use System.Text.Json to test-parse in a diagnostic step: JsonSerializer.Deserialize<T>(json) and inspect the JsonException for the offending path","Enable strict mode off (default) to get a default value instead of an exception, or fix the payload in strict mode","Check serializerOptions (naming policy, converters) match the payload conventions"],"exampleFix":"// before\nvar item = ObjectConverter.ConvertTo<TaskList>(rawJson, typeof(TaskList));\n// after\nif (typeof(TaskList).IsAssignableFrom(typeof(TaskList)) && !IsValidJson<TaskList>(rawJson))\n    throw new FormatException(\"Payload shape does not match TaskList\");\nvar item = ObjectConverter.ConvertTo<TaskList>(rawJson, typeof(TaskList));","handlingStrategy":"validation","validationCode":"bool isValidJson<T>(string s) { try { JsonSerializer.Deserialize<T>(s); return true; } catch (JsonException) { return false; } }","typeGuard":"bool looksLikeJson(string s) => !string.IsNullOrWhiteSpace(s) && (s.TrimStart().StartsWith('{') || s.TrimStart().StartsWith('['));","tryCatchPattern":"try { value = ObjectConverter.ConvertTo(targetType, json); }\ncatch (TypeConversionException ex) { logger.LogError(ex, \"Deserialization failed for {Target}\", targetType); value = null; }","preventionTips":["Validate JSON shape against the target schema before binding","Keep serializer options (naming policies) consistent between producer and consumer","Check the inner exception path info to find the mismatched property"],"tags":["csharp","json","type-conversion","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"}