microsoft/aspire · error · InvalidOperationException

The configuration file

Error message

The configuration file '{0}' contains invalid JSON: {1}

What it means

ConfigurationHelper.AddSettingsFile loads a user settings file into the CLI configuration by parsing it as JSON. If System.Text.Json raises a JsonException while reading the file, the helper wraps it in an InvalidOperationException that includes the file path and the parser's message, so users know which settings file is malformed rather than getting a bare parse error.

Solutions

  1. Open the settings file named in the message and fix the JSON at the position given by the parser message (e.g. remove trailing commas, close braces).
  2. Validate the file with a JSON linter or `cat file | python -m json.tool` before rerunning the command.
  3. If you cannot repair it, delete or restore the settings file and let the CLI regenerate defaults; re-apply settings carefully.
  4. Restore from git or a backup if the file was broken by a merge conflict.

Example fix

// before (.aspire/settings.json)
{
  "features": { "updateNotificationsEnabled": true, },
// after
{
  "features": { "updateNotificationsEnabled": true }
}
Defensive patterns

Strategy: validation

Validate before calling

try { using var doc = JsonDocument.Parse(File.ReadAllText(settingsPath)); }
catch (JsonException ex) { Console.WriteLine($"{settingsPath} has invalid JSON: {ex.Message}"); }

Try / catch

try { /* run aspire command */ }
catch (InvalidOperationException ex) when (ex.Message.Contains("invalid JSON")) { Console.Error.WriteLine(ex.Message); /* point user at the file/offset */ }

Prevention

When it happens

Trigger: Calling CLI commands that register settings files (via RegisterSettingsFile) when a settings file on disk (e.g. .aspire/settings.json) contains syntactically invalid JSON - trailing commas, unquoted keys, comments where not allowed, truncated edits.

Common situations: Hand-editing aspire settings.json and leaving a trailing comma or missing brace, a merge conflict marker left inside the file, an editor writing BOM/encoding issues, or an interrupted write leaving a truncated file.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/98fe9437d767edac. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Cli/Utils/ConfigurationHelper.cs:277

        // Pre-process the file to handle comments and trailing commas.
        // Microsoft.Extensions.Configuration.Json doesn't support JSON comments,
        // so we parse with comment support and load the clean JSON via stream.
        try
        {
            var content = File.ReadAllText(filePath);
            var node = JsonNode.Parse(content, documentOptions: ParseOptions);
            if (node is not null)
            {
                var cleanJson = node.ToJsonString(new JsonSerializerOptions { WriteIndented = true });
                var bytes = System.Text.Encoding.UTF8.GetBytes(cleanJson);
                configuration.AddJsonStream(new MemoryStream(bytes));
                return;
            }
        }
        catch (JsonException ex)
        {
            throw new InvalidOperationException(
                string.Format(CultureInfo.CurrentCulture, ErrorStrings.InvalidJsonInConfigFile, filePath, ex.Message),
                ex);
        }

        configuration.AddJsonFile(filePath, optional: true);
    }

    /// <summary>
    /// Normalizes a settings file by converting flat colon-separated keys to nested JSON objects.
    /// </summary>
    internal static bool TryNormalizeSettingsFile(string filePath)
    {
        try
        {
            var content = File.ReadAllText(filePath);

            if (string.IsNullOrWhiteSpace(content))
            {

View on GitHub (pinned to 25830f84bd)