{"record":{"id":"f1e6875c01cf1a9b","repo":"JamesNK/Newtonsoft.Json","slug":"the-jsonreader-should-not-be-on-a-token-of-type-0-f1e687","errorCode":null,"errorMessage":"The JsonReader should not be on a token of type {0}.","messagePattern":"The JsonReader should not be on a token of type (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JContainer.cs","lineNumber":905,"sourceCode":"                        break;\n                    case JsonToken.Undefined:\n                        v = JValue.CreateUndefined();\n                        v.SetLineInfo(lineInfo, settings);\n                        parent.Add(v);\n                        break;\n                    case JsonToken.PropertyName:\n                        JProperty? property = ReadProperty(r, settings, lineInfo, parent);\n                        if (property != null)\n                        {\n                            parent = property;\n                        }\n                        else\n                        {\n                            r.Skip();\n                        }\n                        break;\n                    default:\n                        throw new InvalidOperationException(\"The JsonReader should not be on a token of type {0}.\".FormatWith(CultureInfo.InvariantCulture, r.TokenType));\n                }\n            } while (r.Read());\n        }\n\n        private static JProperty? ReadProperty(JsonReader r, JsonLoadSettings? settings, IJsonLineInfo? lineInfo, JContainer parent)\n        {\n            DuplicatePropertyNameHandling duplicatePropertyNameHandling = settings?.DuplicatePropertyNameHandling ?? DuplicatePropertyNameHandling.Replace;\n\n            JObject parentObject = (JObject)parent;\n            string propertyName = r.Value!.ToString()!;\n            JProperty? existingPropertyWithName = parentObject.Property(propertyName, StringComparison.Ordinal);\n            if (existingPropertyWithName != null)\n            {\n                if (duplicatePropertyNameHandling == DuplicatePropertyNameHandling.Ignore)\n                {\n                    return null;\n                }\n                else if (duplicatePropertyNameHandling == DuplicatePropertyNameHandling.Error)","sourceCodeStart":887,"sourceCodeEnd":923,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JContainer.cs#L887-L923","documentation":"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.","triggerScenarios":"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.","commonSituations":"Custom JsonReader implementations; corrupt reader state; reading from a partially consumed source; library version mismatch.","solutions":["Use JsonTextReader over valid JSON text.","Do not pre-advance the reader; let Load drive the reads from StartArray/StartObject.","If you use a custom reader, restrict its output to the standard JsonToken values."],"exampleFix":"// before\nJArray.Load(customReaderInBadState);\n// after\nusing var reader = new JsonTextReader(new StringReader(jsonText));\nJArray.Load(reader);","handlingStrategy":"try-catch","validationCode":"if (reader.TokenType != JsonToken.StartArray && reader.TokenType != JsonToken.StartObject && reader.TokenType != JsonToken.StartConstructor && reader.TokenType != JsonToken.None) {\n    throw new InvalidOperationException(\"Reader not positioned at a start token.\");\n}\nJArray.Load(reader);","typeGuard":null,"tryCatchPattern":"try { var token = JArray.Load(reader); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"should not be on a token of type\")) {\n    // re-create a JsonTextReader from the original text and reload\n}","preventionTips":["Use JsonTextReader over valid JSON for sync loads.","Let Load drive the reader; don't pre-advance to unexpected tokens.","Restrict custom readers to standard JsonToken values."],"tags":["jsonreader","sync","invalid-operation","parsing"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}