JamesNK/Newtonsoft.Json · error · ArgumentException

Could not determine JSON object type for type {0}.

Error message

Could not determine JSON object type for type {0}.

What it means

ValidateContent (invoked by Merge) throws ArgumentException when the content is neither a JToken subclass nor multi-content (an IEnumerable that is not string, JToken, or byte[]). Merge can only combine JSON tokens or enumerables of them.

Source

Thrown at Src/Newtonsoft.Json/Linq/JContainer.cs:774

                return;
            }

            ValidateContent(content);
            MergeItem(content, settings);
        }

        private void ValidateContent(object content)
        {
            if (content.GetType().IsSubclassOf(typeof(JToken)))
            {
                return;
            }
            if (IsMultiContent(content))
            {
                return;
            }

            throw new ArgumentException("Could not determine JSON object type for type {0}.".FormatWith(CultureInfo.InvariantCulture, content.GetType()), nameof(content));
        }

        internal void ReadTokenFrom(JsonReader reader, JsonLoadSettings? options)
        {
            int startDepth = reader.Depth;

            if (!reader.Read())
            {
                throw JsonReaderException.Create(reader, "Error reading {0} from JsonReader.".FormatWith(CultureInfo.InvariantCulture, GetType().Name));
            }

            ReadContentFrom(reader, options);

            int endDepth = reader.Depth;

            if (endDepth > startDepth)
            {
                throw JsonReaderException.Create(reader, "Unexpected end of content while loading {0}.".FormatWith(CultureInfo.InvariantCulture, GetType().Name));

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Convert the value first: Merge(JToken.FromObject(poco)) or Merge(new JValue(42)).
  2. For collections of CLR objects, project to JTokens (e.g. items.Select(JToken.FromObject)) before merging.
  3. Use JsonConvert serialization then JToken.Parse if you need full serializer behavior.

Example fix

// before
jObject.Merge(myPoco);
// after
jObject.Merge(JToken.FromObject(myPoco));
Defensive patterns

Strategy: validation

Validate before calling

object safe = content;
if (!(content is JToken) && !IsMultiContent(content)) {
    safe = JToken.FromObject(content);
}
container.Merge(safe);

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

Type guard

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

Try / catch

try { container.Merge(content); }
catch (ArgumentException ex) when (ex.Message.Contains("Could not determine JSON object type")) {
    container.Merge(JToken.FromObject(content));
}

Prevention

When it happens

Trigger: jObject.Merge(42); jArray.Merge(new DateTime()); Merge(someRandomPoco) where the poco is not a JToken or IEnumerable.

Common situations: Assuming Merge accepts arbitrary CLR objects like Add/FromObject; passing a primitive or DTO directly.

Related errors


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