{"record":{"id":"187ca3e6728e2295","repo":"OrchardCMS/OrchardCore","slug":"cannot-convert-value-to-guid","errorCode":null,"errorMessage":"Cannot convert {value} to Guid","messagePattern":"Cannot convert (.+?) to Guid","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs","lineNumber":351,"sourceCode":"    {\n        return value?._jsonValue?.GetValue<decimal?>();\n    }\n\n    public static explicit operator double(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<double>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Double\");\n    }\n\n    public static explicit operator double?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<double?>();\n    }\n\n    public static explicit operator Guid(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<Guid>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Guid\");\n    }\n\n    public static explicit operator Guid?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<Guid?>();\n    }\n\n    public static explicit operator short(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<short>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Int16\");\n    }\n\n    public static explicit operator short?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<short?>();\n    }\n","sourceCodeStart":333,"sourceCodeEnd":369,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs#L333-L369","documentation":"The explicit operator Guid on JsonDynamicValue converts via GetValue<Guid>() and throws InvalidCastException when the result is null or the JSON token is not a parseable GUID string. It fails fast with a descriptive message; use the (Guid?) operator for non-throwing access.","triggerScenarios":"Executing `(Guid)jsonDynamicValue` where the wrapper/_jsonValue is null or the token is a string not in a GUID-parseable format, a number, boolean, object, or array.","commonSituations":"Identifier fields (content item IDs, relation keys) in JSON that are null, empty, or stored in a non-GUID format (e.g. integers or short slugs); data migrated from another system that used different ID formats.","solutions":["Confirm the JSON value is a valid GUID string (32 hex digits / with-dashes format) before casting","Use the nullable cast (Guid?) and treat null as 'no identifier'","Validate with Guid.TryParse(value.ToString(), out var g) first, especially if IDs may be non-GUID strings","Null-check the wrapper before casting"],"exampleFix":"// before\nvar id = (Guid)jsonDynamicValue[\"ContentItemId\"];\n// after\nvar id = (Guid?)jsonDynamicValue[\"ContentItemId\"] ?? Guid.NewGuid();","handlingStrategy":"type-guard","validationCode":"if (jsonDynamicValue is null) throw new InvalidOperationException(\"null wrapper\");\nvar raw = jsonDynamicValue.ToString();\nif (!Guid.TryParse(raw, out _))\n    throw new InvalidOperationException($\"'{raw}' is not a GUID\");\nvar g = (Guid)jsonDynamicValue;","typeGuard":"static bool TryGetGuid(JsonDynamicValue v, out Guid value)\n{\n    value = default;\n    if (v is null) return false;\n    var g = (Guid?)v;\n    if (g is null) return false;\n    value = g.Value;\n    return true;\n}","tryCatchPattern":"Guid id;\ntry\n{\n    id = (Guid)jsonDynamicValue[\"ContentItemId\"];\n}\ncatch (InvalidCastException ex)\n{\n    id = Guid.Empty;\n}","preventionTips":["Use (Guid?) when an identifier may be missing","Validate identifier format at the boundary before dynamic casting","Do not repurpose GUID fields to hold non-GUID values (slugs, ints)","Assert expected JSON schema in integration tests"],"tags":["casting","invalidcastexception","json","guid"],"backgroundTag":"type-mismatch","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}