I am trying to hide a nested class from a Web API response if that nested classes is null.
Please find the Web API model below:
public class School {
public Student students {get; set;}
public Teacher teachers {get; set;}
}
public class Student {
public string id {get; set;}
public string name {get; set;}
}
public class Teacher {
public string id {get; set;}
public string name {get; set;}
}
Postman Response (if the teacher value is null):
{
students: {
id: "1234",
name: "Alex"
},
teachers: null
}
But my expected response is:
{
students: {
id: "1234",
name: "Alex"
}
}
I've tried [DataMember(EmitDefaultValues = False)], [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] but nothing worked. Please advice me how to hide the inner class model if it is null.