{"record":{"id":"59c3ce30e06aad0a","repo":"microsoft/aspire","slug":"json-patch-path-segment-segment-refers-to-a-null-value","errorCode":null,"errorMessage":"JSON Patch path segment '{segment}' refers to a null value.","messagePattern":"JSON Patch path segment '(.+?)' refers to a null value\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/Dcp/JsonPatch.cs","lineNumber":229,"sourceCode":"            default:\n                throw new JsonException(\"A JSON Patch path can only traverse objects and arrays.\");\n        }\n\n        return current;\n    }\n\n    private static JsonNode GetParent(JsonNode? current, string[] segments)\n    {\n        var parent = current ?? throw new JsonException(\"A JSON Patch path cannot traverse a null value.\");\n\n        for (var index = 0; index < segments.Length - 1; index++)\n        {\n            var segment = segments[index];\n            parent = parent switch\n            {\n                JsonObject jsonObject when jsonObject.TryGetPropertyValue(segment, out var child) && child is not null => child,\n                JsonArray jsonArray => jsonArray[ParseArrayIndex(segment, jsonArray.Count, allowEnd: false)]\n                    ?? throw new JsonException($\"JSON Patch path segment '{segment}' refers to a null value.\"),\n                _ => throw new JsonException($\"JSON Patch path segment '{segment}' does not exist.\"),\n            };\n        }\n\n        return parent;\n    }\n\n    private static void ApplyToObject(JsonObject target, string operation, string propertyName, JsonNode? value)\n    {\n        switch (operation)\n        {\n            case \"add\":\n                target[propertyName] = value?.DeepClone();\n                break;\n            case \"remove\":\n                if (!target.Remove(propertyName))\n                {\n                    throw new JsonException($\"JSON Patch remove path refers to missing property '{propertyName}'.\");","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/Dcp/JsonPatch.cs#L211-L247","documentation":"While resolving a patch path's parent, GetParent indexes into a JsonArray for a segment. If the element at that index is JSON null, the traversal cannot continue and the library throws this JsonException naming the offending segment. It distinguishes 'element exists but is null' from 'segment missing'.","triggerScenarios":"A patch path like '/containers/2/name' where containers[2] is JSON null — typically when the array holds explicit null placeholders.","commonSituations":"Arrays that were pre-sized with nulls; deserialized documents where list entries were nulled by a previous transform or merge.","solutions":["Remove the null element or replace it with an object before applying the patch","Rewrite the path to target an existing, non-null element","Use an 'add' operation with the '-' index to append instead of addressing a null slot"],"exampleFix":"// before\narray[2] = null; // then patching /items/2/name fails\n// after\narray[2] = new JsonObject { [\"name\"] = \"placeholder\" };","handlingStrategy":"validation","validationCode":"static bool ArrayElementExists(JsonArray array, int index) => index >= 0 && index < array.Count && array[index] is not null;","typeGuard":null,"tryCatchPattern":"try { patcher.Apply(doc, operations); }\ncatch (JsonException ex) when (ex.Message.Contains(\"refers to a null value\")) {\n    logger.LogWarning(ex, \"Patch path hit a null array element; sanitize the array first\");\n}","preventionTips":["Do not store explicit JSON null placeholders in arrays that get patched","Sanitize arrays (remove nulls) before applying patches","Prefer appending with '-' over addressing slots"],"tags":["json-patch","null-reference","json"],"backgroundTag":"null-argument","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}