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);
}
}
}
}
#endifView on GitHub (pinned to 4f73e74372)
Solutions
- Inspect InnerException to identify the true getter failure.
- Guard the getter against null dependencies or wrap its body so it doesn't throw during serialization.
- Exclude the throwing property from serialization with [JsonIgnore] or NullValueHandling/conditional serialization (ShouldSerializeX / ShouldSerialize prefix).
- 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
- Make property getters safe: guard against null/disposed dependencies.
- Use ShouldSerializeX / ShouldSerialize prefix to skip members whose state isn't ready.
- Decorate volatile getters with [JsonIgnore].
- Set JsonSerializerSettings.Error to handle and continue.
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
- Error setting value to '{0}' on '{1}'.
- Error getting value from '{0}' on '{1}'.
- Error getting value from '{0}' on '{1}'.
- Error setting value to '{0}' on '{1}'.
- Could not create getter for {0}. ByRef return values are not
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/e4a9de5aa39844f1.
Report an issue: GitHub.