microsoft/aspire · error · JsonException
Cannot convert to boolean for key
Error message
Cannot convert {reader.TokenType} to boolean for key '{key}' What it means
Inside the dictionary converter, each property value must be a JSON true/false literal or a 'true'/'false' string; any other token (number, null, object, array) throws JsonException naming the token and key. The converter deliberately only flexes for string booleans.
Solutions
- Replace the numeric/null value with true or false (or the strings "true"/"false") for the key named in the message.
- If the value legitimately needs to be non-boolean, move it to a different config key, not the boolean dictionary.
- If 1/0 support is desired, extend the switch's ParseBooleanString path to accept numeric tokens.
Example fix
// before
"featureFlags": { "MyFeature": 1 }
// after
"featureFlags": { "MyFeature": true } Defensive patterns
Strategy: validation
Validate before calling
using var doc = JsonDocument.Parse(json);
foreach (var p in doc.RootElement.GetProperty("featureFlags").EnumerateObject())
{
if (p.Value.ValueKind is not (JsonValueKind.True or JsonValueKind.False or JsonValueKind.String))
throw new FormatException($"Feature flag '{p.Name}' must be true/false or \"true\"/\"false\".");
} Try / catch
try
{
var config = JsonSerializer.Deserialize<MyConfig>(json, options);
}
catch (JsonException ex) when (ex.Message.Contains("Cannot convert") && ex.Message.Contains("to boolean"))
{
logger.LogError(ex, "Config dictionary contains a value that is not a boolean.");
} Prevention
- Use true/false (or "true"/"false") only — never 1/0 or null for boolean flags.
- Coerce numeric boolean values at ingestion points (env var readers, scripts).
- Document the expected config schema for team members.
When it happens
Trigger: A value in a Dictionary<string,bool> config object that is a number (1/0), null, nested object, or array, e.g. "featureFlags": { "MyFeature": 1 }.
Common situations: Users familiar with other config systems writing 1/0 or yes/no; values interpolated from env vars carrying raw numbers; schema assumptions from other tooling.
Understand the failure class
Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.
Related errors
- Cannot convert string
- ' ' must be a JSON string.
- ' ' must be a JSON string.
- ' ' must contain a JSON object.
- Expected StartObject, got
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/3092c5673d08dfad.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Cli/Configuration/FlexibleBooleanDictionaryConverter.cs:51
{
return dictionary;
}
if (reader.TokenType != JsonTokenType.PropertyName)
{
throw new JsonException($"Expected PropertyName, got {reader.TokenType}");
}
var key = reader.GetString() ?? throw new JsonException("Property name cannot be null");
reader.Read();
bool value = reader.TokenType switch
{
JsonTokenType.True => true,
JsonTokenType.False => false,
JsonTokenType.String => ParseBooleanString(reader.GetString(), key),
_ => throw new JsonException($"Cannot convert {reader.TokenType} to boolean for key '{key}'")
};
dictionary[key] = value;
}
throw new JsonException("Unexpected end of JSON");
}
public override void Write(Utf8JsonWriter writer, Dictionary<string, bool> value, JsonSerializerOptions options)
{
writer.WriteStartObject();
foreach (var kvp in value)
{
writer.WriteBoolean(kvp.Key, kvp.Value);
}
writer.WriteEndObject();View on GitHub (pinned to 25830f84bd)