elsa-workflows/elsa-core · error · JsonException

Unknown token

Error message

Unknown token {reader.TokenType}

What it means

ExpandoObjectConverter.Read deserializes JSON into an IDictionary<string,object> backed by ExpandoObject, handling only the token types it knows (StartObject, StartArray, primitives, etc.). If the reader lands on a JsonTokenType it does not handle (default branch), it throws JsonException("Unknown token ..."). This indicates malformed or structurally unexpected JSON.

Solutions

  1. Validate the JSON structure of the property/state payload before deserialization.
  2. Fix misnested braces/keys in the JSON source.
  3. Re-materialize the payload from a trusted export (Studio or workflow export API).
  4. If a custom converter chains into this one, verify the reader position it hands over is on a supported token.

Example fix

// before (invalid: object used as key position)
{ ["key"]: "value" }

// after
{ "key": "value" }
Defensive patterns

Strategy: validation

Validate before calling

using var doc = JsonDocument.Parse(json);
if (doc.RootElement.ValueKind == JsonValueKind.Array || doc.RootElement.ValueKind == JsonValueKind.String)
    throw new InvalidOperationException("Expected a JSON object for property dictionary");

Type guard

static bool IsJsonObject(string s) { try { using var d = JsonDocument.Parse(s); return d.RootElement.ValueKind == JsonValueKind.Object; } catch { return false; } }

Try / catch

try { var props = JsonSerializer.Deserialize<IDictionary<string, object>>(json, options); } catch (JsonException ex) when (ex.Message.StartsWith("Unknown token")) { log.LogError(ex, "Unexpected token in property dictionary JSON"); }

Prevention

When it happens

Trigger: Deserializing a property dictionary where the reader is positioned on an unexpected token such as PropertyName, EndObject, or None — typically malformed nesting or a value appearing where a key was expected (src/modules/Elsa.Workflows.Core/Serialization/Converters/ExpandoObjectConverter.cs:82).

Common situations: Corrupted workflow instance state JSON; deserializing JSON with duplicate/misnested keys; feeding JSON5-style content into a strict System.Text.Json reader.

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 elsa-workflows/elsa-core@fe9217bdfa (2026-09-13). Data as JSON: /api/errors/97c635ff255d8b69. Report an issue: GitHub.

Appendix: source

Thrown at src/modules/Elsa.Workflows.Core/Serialization/Converters/ExpandoObjectConverter.cs:82

                {
                    switch (reader.TokenType)
                    {
                        case JsonTokenType.EndObject:
                            return dict;
                        case JsonTokenType.PropertyName:
                            var key = reader.GetString()!;
                            reader.Read();
                            var value = Read(ref reader, typeof(object), options)!;
                            dict.Add(key, value);
                            break;
                        default:
                            throw new JsonException();
                    }
                }

                throw new JsonException();
            default:
                throw new JsonException($"Unknown token {reader.TokenType}");
        }
    }

    private IDictionary<string, object> CreateDictionary() => new ExpandoObject()!;
}

View on GitHub (pinned to fe9217bdfa)