JamesNK/Newtonsoft.Json · error · JsonSerializationException

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

Error message

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

What it means

Identical to error 221 but from ExpressionValueProvider.GetValue on platforms where the expression-tree provider is active. Wraps the original getter exception as InnerException; '{0}' is the member name, '{1}' the target type.

Source

Thrown at Src/Newtonsoft.Json/Serialization/ExpressionValueProvider.cs:110

        /// <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 = ExpressionReflectionDelegateFactory.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. Read InnerException for the true getter error.
  2. Add a null/state guard inside the getter or use ShouldSerializeX to skip it.
  3. Decorate the problematic property with [JsonIgnore].
  4. Configure JsonSerializerSettings.Error handler to continue past the failing member.

Example fix

// before
public class Entity { public string Name => _lazy.Value.Name; }
JsonConvert.Serialize(entity); // _lazy throws
// after
public class Entity {
    public bool ShouldSerializeName() => _lazy.IsValueCreated;
    public string Name => _lazy.Value.Name;
}
Defensive patterns

Strategy: validation

Validate before calling

public bool ShouldSerializeX() => _lazy != null && _lazy.IsValueCreated;

Try / catch

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

Prevention

When it happens

Trigger: A property getter throws while serializing, surfaced through the expression-based value provider. Same conditions as 221 but on .NET Core/.NET 5+ runtimes.

Common situations: EF/Linq2Sql proxy properties throwing after context disposal, computed properties throwing on null state, porting code from .NET Framework where DynamicValueProvider was used and now ExpressionValueProvider surfaces the failure.

Related errors


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