JamesNK/Newtonsoft.Json · error · JsonReaderException

CurrentPropertyName has not been set on scope.

Error message

CurrentPropertyName has not been set on scope.

What it means

Thrown by JsonValidatingReader's CurrentSchemas getter when the current token is an Object but the scope's CurrentPropertyName is null. The schema validator needs the current property name to look up the matching property/patternProperty schema; reaching this state with a null name indicates an internal reader-state inconsistency (property value being validated before a property name was recorded). It surfaces as a JsonReaderException.

Source

Thrown at Src/Newtonsoft.Json/JsonValidatingReader.cs:194

                if (_currentScope == null)
                {
                    return new List<JsonSchemaModel>(new[] { _model });
                }

                if (_currentScope.Schemas == null || _currentScope.Schemas.Count == 0)
                {
                    return EmptySchemaList;
                }

                switch (_currentScope.TokenType)
                {
                    case JTokenType.None:
                        return _currentScope.Schemas;
                    case JTokenType.Object:
                        {
                            if (_currentScope.CurrentPropertyName == null)
                            {
                                throw new JsonReaderException("CurrentPropertyName has not been set on scope.");
                            }

                            IList<JsonSchemaModel> schemas = new List<JsonSchemaModel>();

                            foreach (JsonSchemaModel schema in CurrentSchemas)
                            {
                                if (schema.Properties != null && schema.Properties.TryGetValue(_currentScope.CurrentPropertyName, out JsonSchemaModel propertySchema))
                                {
                                    schemas.Add(propertySchema);
                                }
                                if (schema.PatternProperties != null)
                                {
                                    foreach (KeyValuePair<string, JsonSchemaModel> patternProperty in schema.PatternProperties)
                                    {
                                        if (Regex.IsMatch(_currentScope.CurrentPropertyName, patternProperty.Key))
                                        {
                                            schemas.Add(patternProperty.Value);
                                        }

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Feed JsonValidatingReader from a normal JsonTextReader (or a reader that faithfully emits PropertyName then value tokens).
  2. Verify the JSON is well-formed and that object members are name/value pairs before validation.
  3. If you wrote a custom reader, ensure WriteToken/Read ordering emits PropertyName before its value.

Example fix

// before
using var validating = new JsonValidatingReader(customReorderingReader);

// after
using var validating = new JsonValidatingReader(new JsonTextReader(input));
Defensive patterns

Strategy: try-catch

Validate before calling

if (validating.TokenType == JsonToken.StartObject &&
    /* ensure a PropertyName precedes value reads */
    string.IsNullOrEmpty(validating.CurrentPath))
{
    // do not validate out-of-order object members
}

Type guard

static bool ReaderStateConsistent(JsonValidatingReader r)
    => !(r.TokenType == JsonToken.StartObject && /* name pending */ false);

Try / catch

try
{
    while (validating.Read()) { /* validate */ }
}
catch (JsonReaderException ex) when (ex.Message.Contains("CurrentPropertyName has not been set"))
{
    // log and surface a clearer 'malformed JSON / reader misorder' message
}

Prevention

When it happens

Trigger: Validating JSON whose object scopes trigger schema lookups before a property name token was processed; custom JsonReader wrappers that advance tokens out of order under a JsonValidatingReader; corrupted/malformed token streams fed to the validating reader.

Common situations: Wrapping a JsonValidatingReader around a non-standard reader; bugs in token ordering from pre-processing/transforming readers; malformed JSON that desynchronizes property-name/value pairing.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/2783cf79f3e8b9ce. Report an issue: GitHub.