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
- Validate the JSON structure of the property/state payload before deserialization.
- Fix misnested braces/keys in the JSON source.
- Re-materialize the payload from a trusted export (Studio or workflow export API).
- 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
- Keep property/state payloads strictly as key/value JSON objects
- Avoid feeding JSON5 or comment-bearing JSON into strict System.Text.Json
- Detect and repair corrupted instance state blobs before deserialization
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
- Runtime entity definition document
- Runtime entity instance document
- Failed to parse JsonDocument
- Failed to extract activity type property
- Expected start of object.
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)