{"record":{"id":"2ffe1f22f6286c63","repo":"JamesNK/Newtonsoft.Json","slug":"object-serialized-to-0-jarray-instance-expected","errorCode":null,"errorMessage":"Object serialized to {0}. JArray instance expected.","messagePattern":"Object serialized to (.+?)\\. JArray instance expected\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JArray.cs","lineNumber":216,"sourceCode":"        {\n            return FromObject(o, JsonSerializer.CreateDefault());\n        }\n\n        /// <summary>\n        /// Creates a <see cref=\"JArray\"/> from an object.\n        /// </summary>\n        /// <param name=\"o\">The object that will be used to create <see cref=\"JArray\"/>.</param>\n        /// <param name=\"jsonSerializer\">The <see cref=\"JsonSerializer\"/> that will be used to read the object.</param>\n        /// <returns>A <see cref=\"JArray\"/> with the values of the specified object.</returns>\n        [RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]\n        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]\n        public new static JArray FromObject(object o, JsonSerializer jsonSerializer)\n        {\n            JToken token = FromObjectInternal(o, jsonSerializer);\n\n            if (token.Type != JTokenType.Array)\n            {\n                throw new ArgumentException(\"Object serialized to {0}. JArray instance expected.\".FormatWith(CultureInfo.InvariantCulture, token.Type));\n            }\n\n            return (JArray)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.WriteStartArray();\n\n            for (int i = 0; i < _values.Count; i++)\n            {","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JArray.cs#L198-L234","documentation":"Thrown by JArray.FromObject when the serialized object does not produce a JSON array. FromObject runs the input through a JsonSerializer (FromObjectInternal) and requires the resulting token's Type to be JTokenType.Array; an object, scalar, or null token is rejected with an ArgumentException naming the actual serialized type.","triggerScenarios":"Calling JArray.FromObject(singlePoco) where the POCO serializes to a JSON object; JArray.FromObject(\"text\") or JArray.FromObject(42) producing a scalar; passing an existing JObject as the source object.","commonSituations":"Treating FromObject as a generic converter and passing a single item instead of a collection; an API response whose shape changed from array to object after a version bump; reusing JObject-style code against JArray.","solutions":["Pass an IEnumerable/List/array (e.g. new[] { item }) so the serializer emits a JSON array.","If the input shape is uncertain, use JToken.FromObject(o) and branch on token.Type before casting to JArray.","Confirm the runtime type and expected JSON shape; deserialize the source directly into List<T> with JsonConvert.DeserializeObject<List<T>>."],"exampleFix":"// before\nJArray arr = JArray.FromObject(mySingleObject);\n// after\nJArray arr = JArray.FromObject(new[] { mySingleObject });","handlingStrategy":"validation","validationCode":"object o = GetSource();\nif (o is JToken t) {\n    if (t.Type != JTokenType.Array) throw new InvalidOperationException(\"Source is \" + t.Type);\n    // safe\n}\nelse if (!(o is System.Collections.IEnumerable && !(o is string) && !(o is byte[]))) {\n    // single object: wrap to force array serialization\n    o = new[] { o };\n}\nJArray arr = JArray.FromObject(o);","typeGuard":"static bool SerializesToArray(object o) =>\n    (o is System.Collections.IEnumerable && !(o is string) && !(o is byte[]));","tryCatchPattern":"try { JArray arr = JArray.FromObject(o); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"JArray instance expected\")) {\n    // handle non-array source: wrap, log, or fall back to JToken.FromObject\n}","preventionTips":["Pass List<T>/T[]/IEnumerable to FromObject, never a single POCO.","When the shape is uncertain, use JToken.FromObject and check Type == Array before casting.","Deserialize directly to List<T> with JsonConvert.DeserializeObject<List<T>> if you control the type."],"tags":["jarray","fromobject","serialization","argument"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}