JamesNK/Newtonsoft.Json · error · ArgumentException

Object serialized to {0}. JArray instance expected.

Error message

Object serialized to {0}. JArray instance expected.

What it means

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.

Source

Thrown at Src/Newtonsoft.Json/Linq/JArray.cs:216

        {
            return FromObject(o, JsonSerializer.CreateDefault());
        }

        /// <summary>
        /// Creates a <see cref="JArray"/> from an object.
        /// </summary>
        /// <param name="o">The object that will be used to create <see cref="JArray"/>.</param>
        /// <param name="jsonSerializer">The <see cref="JsonSerializer"/> that will be used to read the object.</param>
        /// <returns>A <see cref="JArray"/> with the values of the specified object.</returns>
        [RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]
        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
        public new static JArray FromObject(object o, JsonSerializer jsonSerializer)
        {
            JToken token = FromObjectInternal(o, jsonSerializer);

            if (token.Type != JTokenType.Array)
            {
                throw new ArgumentException("Object serialized to {0}. JArray instance expected.".FormatWith(CultureInfo.InvariantCulture, token.Type));
            }

            return (JArray)token;
        }

        /// <summary>
        /// Writes this token to a <see cref="JsonWriter"/>.
        /// </summary>
        /// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
        /// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param>
        [RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]
        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
        public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
        {
            writer.WriteStartArray();

            for (int i = 0; i < _values.Count; i++)
            {

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Pass an IEnumerable/List/array (e.g. new[] { item }) so the serializer emits a JSON array.
  2. If the input shape is uncertain, use JToken.FromObject(o) and branch on token.Type before casting to JArray.
  3. Confirm the runtime type and expected JSON shape; deserialize the source directly into List<T> with JsonConvert.DeserializeObject<List<T>>.

Example fix

// before
JArray arr = JArray.FromObject(mySingleObject);
// after
JArray arr = JArray.FromObject(new[] { mySingleObject });
Defensive patterns

Strategy: validation

Validate before calling

object o = GetSource();
if (o is JToken t) {
    if (t.Type != JTokenType.Array) throw new InvalidOperationException("Source is " + t.Type);
    // safe
}
else if (!(o is System.Collections.IEnumerable && !(o is string) && !(o is byte[]))) {
    // single object: wrap to force array serialization
    o = new[] { o };
}
JArray arr = JArray.FromObject(o);

Type guard

static bool SerializesToArray(object o) =>
    (o is System.Collections.IEnumerable && !(o is string) && !(o is byte[]));

Try / catch

try { JArray arr = JArray.FromObject(o); }
catch (ArgumentException ex) when (ex.Message.Contains("JArray instance expected")) {
    // handle non-array source: wrap, log, or fall back to JToken.FromObject
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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