{"record":{"id":"e794c97309fa6aa7","repo":"microsoft/aspire","slug":"the-persisted-scope-is-not-a-json-object","errorCode":null,"errorMessage":"The persisted scope is not a JSON object.","messagePattern":"The persisted scope is not a JSON object\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.Kubernetes/AzureKubernetesEnvironmentResource.AksPipeline.cs","lineNumber":420,"sourceCode":"\n        // Scope is persisted as a JSON string using the same shape produced by\n        // BicepUtilities.SetScopeAsync:\n        //   { \"resourceGroup\": \"shared-rg\", \"subscription\": \"00000000-...\" }\n        // A missing property means the resource did not pin that scope value, so only that value\n        // falls back to global Azure deployment state. Older state without Scope uses the persisted\n        // resource ID so a changed AppHost scope cannot redirect cleanup to another cluster or wait\n        // on provisioning that is not part of the destroy graph.\n        string subscriptionId;\n        string? resourceGroupName;\n        if (deploymentStateSection.Data[\"Scope\"] is not null)\n        {\n            string? scopedSubscription;\n            string? scopedResourceGroup;\n            try\n            {\n                var scopeJson = deploymentStateSection.Data[\"Scope\"]!.GetValue<string>();\n                var scope = JsonNode.Parse(scopeJson)?.AsObject()\n                    ?? throw new InvalidOperationException(\"The persisted scope is not a JSON object.\");\n                scopedSubscription = scope[\"subscription\"]?.GetValue<string>();\n                scopedResourceGroup = scope[\"resourceGroup\"]?.GetValue<string>();\n            }\n            catch (Exception ex) when (ex is not OperationCanceledException)\n            {\n                throw new InvalidOperationException(\n                    $\"The Azure deployment state for AKS environment '{Name}' contains an invalid scope.\",\n                    ex);\n            }\n\n            (subscriptionId, resourceGroupName) = await ResolveDeploymentScopeAsync(\n                scopedSubscription,\n                scopedResourceGroup,\n                context.Services,\n                context.CancellationToken).ConfigureAwait(false);\n        }\n        else\n        {","sourceCodeStart":402,"sourceCodeEnd":438,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.Kubernetes/AzureKubernetesEnvironmentResource.AksPipeline.cs#L402-L438","documentation":"Thrown while parsing the persisted 'Scope' value from the AKS deployment state: the stored value is not a JSON object (JsonNode.Parse returned a non-object or null). The scope must be a JSON object like {\"resourceGroup\":\"shared-rg\",\"subscription\":\"<guid>\"} as written by BicepUtilities.SetScopeAsync.","triggerScenarios":"GetAksCredentialsForDestroyAsync reads deploymentStateSection.Data[\"Scope\"], calls JsonNode.Parse(...).AsObject(), and the parsed node is a string/number/array/null instead of an object.","commonSituations":"The Scope output in the deployment state was corrupted or replaced by a plain string; a custom template wrote the scope without JSON encoding; state was hand-edited or migrated from a different schema version.","solutions":["Inspect the Scope value in the deployment state file and fix it to the JSON object shape {\"resourceGroup\":...,\"subscription\":...}","Re-deploy the environment so BicepUtilities.SetScopeAsync rewrites the scope in the correct shape","If a custom module writes Scope, ensure it stores a JSON-encoded object, not a raw string","Delete stale state and redeploy from scratch"],"exampleFix":"// before (bad stored value)\n// \"Scope\": \"shared-rg/00000000-0000-0000-0000-000000000000\"\n// after (correct)\n// \"Scope\": \"{ \\\"resourceGroup\\\": \\\"shared-rg\\\", \\\"subscription\\\": \\\"00000000-0000-0000-0000-000000000000\\\" }\"","handlingStrategy":"validation","validationCode":"var scopeJson = state.Data[\"Scope\"];\nusing var doc = JsonDocument.Parse(scopeJson);\nif (doc.RootElement.ValueKind != JsonValueKind.Object)\n    throw new InvalidOperationException(\"Scope must be a JSON object with resourceGroup/subscription.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only let BicepUtilities.SetScopeAsync write the Scope value","Validate state schema after migrations or tooling upgrades"],"tags":["azure","aks","json","deployment-state"],"backgroundTag":"json-parse-error","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"}