JamesNK/Newtonsoft.Json · error · InvalidOperationException

The JsonReader should not be on a token of type {0}.

Error message

The JsonReader should not be on a token of type {0}.

What it means

Thrown by the synchronous ReadContentFrom default switch case when the JsonReader is positioned on a token type the loader does not recognize/handle. This is the sync counterpart of the async guard and signals an invalid or unsupported reader state.

Source

Thrown at Src/Newtonsoft.Json/Linq/JContainer.cs:905

                        break;
                    case JsonToken.Undefined:
                        v = JValue.CreateUndefined();
                        v.SetLineInfo(lineInfo, settings);
                        parent.Add(v);
                        break;
                    case JsonToken.PropertyName:
                        JProperty? property = ReadProperty(r, settings, lineInfo, parent);
                        if (property != null)
                        {
                            parent = property;
                        }
                        else
                        {
                            r.Skip();
                        }
                        break;
                    default:
                        throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, r.TokenType));
                }
            } while (r.Read());
        }

        private static JProperty? ReadProperty(JsonReader r, JsonLoadSettings? settings, IJsonLineInfo? lineInfo, JContainer parent)
        {
            DuplicatePropertyNameHandling duplicatePropertyNameHandling = settings?.DuplicatePropertyNameHandling ?? DuplicatePropertyNameHandling.Replace;

            JObject parentObject = (JObject)parent;
            string propertyName = r.Value!.ToString()!;
            JProperty? existingPropertyWithName = parentObject.Property(propertyName, StringComparison.Ordinal);
            if (existingPropertyWithName != null)
            {
                if (duplicatePropertyNameHandling == DuplicatePropertyNameHandling.Ignore)
                {
                    return null;
                }
                else if (duplicatePropertyNameHandling == DuplicatePropertyNameHandling.Error)

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Use JsonTextReader over valid JSON text.
  2. Do not pre-advance the reader; let Load drive the reads from StartArray/StartObject.
  3. If you use a custom reader, restrict its output to the standard JsonToken values.

Example fix

// before
JArray.Load(customReaderInBadState);
// after
using var reader = new JsonTextReader(new StringReader(jsonText));
JArray.Load(reader);
Defensive patterns

Strategy: try-catch

Validate before calling

if (reader.TokenType != JsonToken.StartArray && reader.TokenType != JsonToken.StartObject && reader.TokenType != JsonToken.StartConstructor && reader.TokenType != JsonToken.None) {
    throw new InvalidOperationException("Reader not positioned at a start token.");
}
JArray.Load(reader);

Try / catch

try { var token = JArray.Load(reader); }
catch (InvalidOperationException ex) when (ex.Message.Contains("should not be on a token of type")) {
    // re-create a JsonTextReader from the original text and reload
}

Prevention

When it happens

Trigger: Loading via JArray.Load/JObject.Load/JConstructor.Load with a custom JsonReader yielding an out-of-range token; a reader advanced to an unexpected token before Load is called.

Common situations: Custom JsonReader implementations; corrupt reader state; reading from a partially consumed source; library version mismatch.

Related errors


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