newtonsoft self referencing loop detected with type code example

Example 1: Newtonsoft.Json.JsonSerializationException: 'Self referencing loop detected for property

// It seems that you have a reacurring properties in your object.
// You can either clean up your object and make sure you are not getting
// The properties multiple times OR you can tell your serializer 
// That this is acceptable behavior like this:

var settings = new Newtonsoft.Json.JsonSerializerSettings();
// This tells your serializer that multiple references are okay.
settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

return JsonConvert.SerializeObject(<Your Object>, settings);

Example 2: asp.net core api Self referencing loop detected for property

public class StaffMember
{
    public string FirstName { get; set; }
    public virtual Department Department { get; set; }
}

public class Department
{
    public string DepartmentName { get; set; }
    [JsonIgnore]
    public virtual ICollection StaffMembers { get; set; }
}