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 ReflectionValueProvider.GetValue when ReflectionUtils.GetMemberValue throws while reading a member from the target object. '{0}' is the member name, '{1}' the target type; the original exception is attached as InnerException. This is the reflection-fallback counterpart of errors 221/223.

Source

Thrown at Src/Newtonsoft.Json/Serialization/ReflectionValueProvider.cs:86

        /// 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
            {
                // https://github.com/dotnet/corefx/issues/26053
                if (_memberInfo is PropertyInfo propertyInfo && propertyInfo.PropertyType.IsByRef)
                {
                    throw new InvalidOperationException("Could not create getter for {0}. ByRef return values are not supported.".FormatWith(CultureInfo.InvariantCulture, propertyInfo));
                }

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

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Inspect InnerException to find the real getter exception.
  2. Guard the getter against null/disposed state or wrap the body to never throw during serialization.
  3. Skip the property with [JsonIgnore] or conditional serialization (ShouldSerializeX).
  4. Configure JsonSerializerSettings.Error to log and continue past the failing member.

Example fix

// before: getter throws
public class Conn { public Stream Stream => _disposed ? throw new ObjectDisposedException("x") : _s; }
JsonConvert.Serialize(conn);
// after
public class Conn {
    public bool ShouldSerializeStream() => !_disposed;
    public Stream Stream => _disposed ? null : _s;
}
Defensive patterns

Strategy: validation

Validate before calling

public bool ShouldSerializeX() => !_disposed && _inner != null;

Try / catch

try { JsonConvert.SerializeObject(obj); }
catch (JsonSerializationException ex) when (ex.Message.Contains("Error getting value")) {
    logger.Error(ex.InnerException, "reflection getter failed at {Path}", ex.Path); throw;
}

Prevention

When it happens

Trigger: Serializing an object where reading a field/property via reflection fails: a property getter that throws (NullReferenceException, ObjectDisposedException, etc.), or attempting to read a member that reflection cannot access.

Common situations: EF/proxy property getters throwing after context disposal, computed properties throwing on bad state, restricted reflection environments, or serializing interop types with inaccessible members.

Related errors


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