{"record":{"id":"76e7b96b65a4388d","repo":"JamesNK/Newtonsoft.Json","slug":"object-serialized-to-0-jobject-instance-expecte","errorCode":null,"errorMessage":"Object serialized to {0}. JObject instance expected.","messagePattern":"Object serialized to (.+?)\\. JObject instance expected\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JObject.cs","lineNumber":509,"sourceCode":"        {\n            return FromObject(o, JsonSerializer.CreateDefault());\n        }\n\n        /// <summary>\n        /// Creates a <see cref=\"JObject\"/> from an object.\n        /// </summary>\n        /// <param name=\"o\">The object that will be used to create <see cref=\"JObject\"/>.</param>\n        /// <param name=\"jsonSerializer\">The <see cref=\"JsonSerializer\"/> that will be used to read the object.</param>\n        /// <returns>A <see cref=\"JObject\"/> with the values of the specified object.</returns>\n        [RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]\n        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]\n        public new static JObject FromObject(object o, JsonSerializer jsonSerializer)\n        {\n            JToken token = FromObjectInternal(o, jsonSerializer);\n\n            if (token.Type != JTokenType.Object)\n            {\n                throw new ArgumentException(\"Object serialized to {0}. JObject instance expected.\".FormatWith(CultureInfo.InvariantCulture, token.Type));\n            }\n\n            return (JObject)token;\n        }\n\n        /// <summary>\n        /// Writes this token to a <see cref=\"JsonWriter\"/>.\n        /// </summary>\n        /// <param name=\"writer\">A <see cref=\"JsonWriter\"/> into which this method will write.</param>\n        /// <param name=\"converters\">A collection of <see cref=\"JsonConverter\"/> which will be used when writing the token.</param>\n        [RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]\n        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]\n        public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)\n        {\n            writer.WriteStartObject();\n\n            for (int i = 0; i < _properties.Count; i++)\n            {","sourceCodeStart":491,"sourceCodeEnd":527,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JObject.cs#L491-L527","documentation":"ArgumentException thrown by JObject.FromObject when the source object serializes to a JToken whose type is not Object (e.g. it produced an Array, String, Integer, etc.). FromObject requires the serialized form to be a JSON object; passing something that serializes to an array or primitive fails.","triggerScenarios":"Calling JObject.FromObject on a List/IEnumerable/array (serializes to JArray), on a primitive/string (serializes to JValue), or on an object whose JsonConverter emits a non-object token. Also when a custom JsonSerializer/contract resolver changes the serialized shape.","commonSituations":"Passing a collection where a single object was expected; a converter that flattens an object to a string; wrapping logic that assumes every input is object-shaped.","solutions":["Validate the input type before calling FromObject, or use JToken.FromObject and branch on token.Type.","If the source is a list, deserialize to JArray.FromObject instead.","Adjust or remove the JsonConverter that is altering the serialized shape."],"exampleFix":"// before\nJObject o = JObject.FromObject(myList); // serializes to array -> ArgumentException\n\n// after\nJToken token = JToken.FromObject(myList);\nJObject o = token as JObject;\nif (o == null) throw new InvalidOperationException($\"Expected object, got {token.Type}\");","handlingStrategy":"validation","validationCode":"JToken token = JToken.FromObject(o, serializer);\nif (token.Type != JTokenType.Object)\n    throw new ArgumentException($\"Object serialized to {token.Type}; JObject expected.\", nameof(o));\nreturn (JObject)token;","typeGuard":"static bool SerializesToObject(object o, JsonSerializer s) =>\n    JToken.FromObject(o, s).Type == JTokenType.Object;","tryCatchPattern":"try { return JObject.FromObject(o, serializer); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"JObject instance expected\"))\n{\n    // Source was an array/primitive; handle accordingly.\n}","preventionTips":["Use JToken.FromObject and branch on Type when the input shape is uncertain.","Validate the source is object-shaped before calling JObject.FromObject.","Inspect custom JsonConverters that may alter the serialized shape."],"tags":["newtonsoft-json","linq-to-json","jobject","fromobject","type-mismatch"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}