JamesNK/Newtonsoft.Json · error · ArgumentException
A name is required when setting property name state.
Error message
A name is required when setting property name state.
What it means
Thrown on the synchronous path (JsonWriter.cs) inside InternalWriteToken when writing a JsonToken.PropertyName whose value is not a string. JSON property names are required to be strings; a non-string value is rejected with ArgumentException to prevent malformed output. Identical guard to the async variant.
Source
Thrown at Src/Newtonsoft.Json/JsonWriter.cs:1710
/// <param name="token">The <see cref="JsonToken"/> being written.</param>
/// <param name="value">The value being written.</param>
protected void SetWriteState(JsonToken token, object value)
{
switch (token)
{
case JsonToken.StartObject:
InternalWriteStart(token, JsonContainerType.Object);
break;
case JsonToken.StartArray:
InternalWriteStart(token, JsonContainerType.Array);
break;
case JsonToken.StartConstructor:
InternalWriteStart(token, JsonContainerType.Constructor);
break;
case JsonToken.PropertyName:
if (!(value is string s))
{
throw new ArgumentException("A name is required when setting property name state.", nameof(value));
}
InternalWritePropertyName(s);
break;
case JsonToken.Comment:
InternalWriteComment();
break;
case JsonToken.Raw:
InternalWriteRaw();
break;
case JsonToken.Integer:
case JsonToken.Float:
case JsonToken.String:
case JsonToken.Boolean:
case JsonToken.Date:
case JsonToken.Bytes:
case JsonToken.Null:
case JsonToken.Undefined:View on GitHub (pinned to 4f73e74372)
Solutions
- Pass only string values to WritePropertyName.
- Ensure PropertyName tokens in any JToken/WriteToken pipeline carry a string value.
- Stringify non-string keys before writing (e.g. key.ToString()).
Example fix
// before
writer.WriteToken(JsonToken.PropertyName, 42);
// after
writer.WritePropertyName("42"); Defensive patterns
Strategy: validation
Validate before calling
if (value is string name)
{
writer.WritePropertyName(name);
} Type guard
static bool IsValidPropertyName(object value) => value is string;
Prevention
- Pass only strings to WritePropertyName / WriteToken(PropertyName, ...).
- Convert dictionary keys to strings before writing.
- Validate token values in custom pipelines before writing.
When it happens
Trigger: Calling WritePropertyName with a non-string, or WriteToken/WriteTokenAsync routing a PropertyName token whose value is not a string; programmatic token construction with non-string names.
Common situations: Custom writers/token builders boxing numbers or objects as property names; dictionary serialization with non-string keys not pre-stringified; copy/paste of tokens from a non-conforming source.
Related errors
- A name is required when setting property name state.
- Unexpected value type when writing binary: {0}
- Expected Bytes but got {0}.
- Unexpected value when converting date. Expected DateTime or
- Expected date object value.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/44fb3acf877bcc54.
Report an issue: GitHub.