JamesNK/Newtonsoft.Json · error · JsonSerializationException

Error getting value from '{0}' on '{1}'.

Error message

Error getting value from '{0}' on '{1}'.

What it means

Thrown by DynamicValueProvider.GetValue when the compiled dynamic-method getter throws while reading a member value from the target object during serialization. '{0}' is the member name, '{1}' the target runtime type. The underlying exception is attached as InnerException for diagnosis.

Source

Thrown at Src/Newtonsoft.Json/Serialization/DynamicValueProvider.cs:109

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="target">The target to get the value from.</param>
        /// <returns>The value.</returns>
        public object? GetValue(object target)
        {
            try
            {
                if (_getter == null)
                {
                    _getter = DynamicReflectionDelegateFactory.Instance.CreateGet<object>(_memberInfo);
                }

                return _getter(target);
            }
            catch (Exception ex)
            {
                throw new JsonSerializationException("Error getting value from '{0}' on '{1}'.".FormatWith(CultureInfo.InvariantCulture, _memberInfo.Name, target.GetType()), ex);
            }
        }
    }
}

#endif

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Inspect InnerException to identify the true getter failure.
  2. Guard the getter against null dependencies or wrap its body so it doesn't throw during serialization.
  3. Exclude the throwing property from serialization with [JsonIgnore] or NullValueHandling/conditional serialization (ShouldSerializeX / ShouldSerialize prefix).
  4. Use JsonSerializerSettings.Error to handle and skip the member instead of aborting the whole graph.

Example fix

// before: getter throws when context disposed
public class Order { public Customer Customer => _ctx.Customers.Find(Id); }
JsonConvert.Serialize(order); // throws
// after: guard + ignore
public class Order {
    public bool ShouldSerializeCustomer() => _ctx != null;
    public Customer Customer => _ctx?.Customers.Find(Id);
}
Defensive patterns

Strategy: validation

Validate before calling

public bool ShouldSerializeX() => _dependency != null && _state.IsValid;

Try / catch

try {
    var json = JsonConvert.SerializeObject(obj, settings);
} catch (JsonSerializationException ex) when (ex.Message.Contains("Error getting value")) {
    logger.Error(ex, "Getter failed at {Path}: {Inner}", ex.Path, ex.InnerException?.Message);
    throw;
}

Prevention

When it happens

Trigger: Serializing an object whose property getter throws (e.g. throws NullReferenceException because it dereferences a null sub-object, or throws NotSupportedException). Raised by JsonSerializer.Serialize when the IValueProvider.GetValue call for a member fails.

Common situations: A getter that lazily initializes from a disposed/closed resource (e.g. DataContext after dispose), a computed property that throws when a dependency is null, proxy/entity properties in EF that throw LazyLoadingException after the context is disposed, or a member that returns an incompatible runtime type.

Related errors


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