JamesNK/Newtonsoft.Json · error · ArgumentException

Object serialized to {0}. JObject instance expected.

Error message

Object serialized to {0}. JObject instance expected.

What it means

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.

Source

Thrown at Src/Newtonsoft.Json/Linq/JObject.cs:509

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

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

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

            return (JObject)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.WriteStartObject();

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

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Validate the input type before calling FromObject, or use JToken.FromObject and branch on token.Type.
  2. If the source is a list, deserialize to JArray.FromObject instead.
  3. Adjust or remove the JsonConverter that is altering the serialized shape.

Example fix

// before
JObject o = JObject.FromObject(myList); // serializes to array -> ArgumentException

// after
JToken token = JToken.FromObject(myList);
JObject o = token as JObject;
if (o == null) throw new InvalidOperationException($"Expected object, got {token.Type}");
Defensive patterns

Strategy: validation

Validate before calling

JToken token = JToken.FromObject(o, serializer);
if (token.Type != JTokenType.Object)
    throw new ArgumentException($"Object serialized to {token.Type}; JObject expected.", nameof(o));
return (JObject)token;

Type guard

static bool SerializesToObject(object o, JsonSerializer s) =>
    JToken.FromObject(o, s).Type == JTokenType.Object;

Try / catch

try { return JObject.FromObject(o, serializer); }
catch (ArgumentException ex) when (ex.Message.Contains("JObject instance expected"))
{
    // Source was an array/primitive; handle accordingly.
}

Prevention

When it happens

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

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

Related errors


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