elsa-workflows/elsa-core · error · JsonException

Cannot convert to bool

Error message

Cannot convert {reader.TokenType} to bool

What it means

BooleanConverter is a System.Text.Json JsonConverter<bool> that accepts booleans and bool-like strings ("true"/"false"). When the token is neither a JSON boolean nor a parseable string, Read throws this JsonException, naming the offending token type.

Solutions

  1. Fix the payload to send a real JSON boolean (true/false) or the exact strings "true"/"false".
  2. If the source sends 1/0, switch the property to int or write a custom converter that maps numbers to bool.
  3. If the source sends "yes"/"no" or null, add a tolerant custom JsonConverter<bool> handling those cases.
  4. Log the raw JSON around the failing token to identify which property is malformed.

Example fix

// before
{"enabled": 1}
// after
{"enabled": true}
Defensive patterns

Strategy: validation

Validate before calling

if (node.ValueKind is not (JsonValueKind.True or JsonValueKind.False))
{
    if (node.ValueKind == JsonValueKind.String && bool.TryParse(node.GetString(), out _)) { /* ok */ }
    else throw new FormatException($"Expected bool, got {node.ValueKind}");
}

Type guard

bool IsJsonBool(JsonElement e) => e.ValueKind is JsonValueKind.True or JsonValueKind.False || (e.ValueKind == JsonValueKind.String && bool.TryParse(e.GetString(), out _));

Try / catch

try { var flag = JsonSerializer.Deserialize<bool>(json, options); }
catch (JsonException ex) { logger.LogError(ex, "Invalid boolean in payload"); }

Prevention

When it happens

Trigger: Deserializing a JSON property bound to a bool where the token is a number (e.g. 1), null, an object, an array, or a non-boolean string like "yes"/"True " that bool.TryParse rejects.

Common situations: APIs that send booleans as 1/0; form data serialized as "yes"/"no"; null arriving at a non-nullable bool property; culture/ casing variations like "TRUE".

Understand the failure class

Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.

Related errors


AI-assisted analysis of elsa-workflows/elsa-core@fe9217bdfa (2026-09-13). Data as JSON: /api/errors/69666efb5c1cf5ba. Report an issue: GitHub.

Appendix: source

Thrown at src/modules/Elsa.Common/Converters/BooleanConverter.cs:22

namespace Elsa.Common.Converters;

public class BooleanConverter : JsonConverter<bool>
{
    public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        switch (reader.TokenType)
        {
            case JsonTokenType.True:
                return true;
            case JsonTokenType.False:
                return false;
            case JsonTokenType.String:
                var value = reader.GetString();
                if (bool.TryParse(value, out var b))
                    return b;
                break;
        }
        throw new JsonException($"Cannot convert {reader.TokenType} to bool");
    }

    public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
    {
        writer.WriteBooleanValue(value);
    }
}

View on GitHub (pinned to fe9217bdfa)