-1

Is there any mechanism in c# to ignore List if its null?

public class VehicleResponse 
  {
        public Pagination pagination { get; set; }
        public Vehicles data { get; set; }        
        public Include include { get; set; }
    }

Here Include class property might be null. If it's null then i want to ignore that property.

 public class Include
    {
        
        public List<Devices> devices { get; set; }
        public List<Institutions> institutions { get; set; }
        public List<Cars> cars{ get; set; }
    }

This is the class of include. If any List like devices or institution or cars is null then it should ignored.

I don't want to display null value in my json response. If they null just ignore them.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Nirmal
  • 41
  • 6
  • 1
    "I don't want to display null value in my json response" - that's the crucial part. (There's no concept of *generally* ignoring a property.) Now, which JSON serializer are you using? There's definitely a setting for this in Json.NET (Newtonsoft.Json). I'm not sure about System.Text.Json. – Jon Skeet Aug 13 '20 at 09:34
  • 1
    _"I don't want to display null value in my json response."_ - you might want to make this a little bit more obvious. I was really confused about what you meant by "ignore" until the last line. You might also want to tag your question with the appropriate JSON serializer you're using. Since you've tagged ASP.NET Core, this is likely to be JSON.NET or System.Text.Json. – ProgrammingLlama Aug 13 '20 at 09:34
  • { "pagination": { "offset": 1, "limit": 4, "total": 1 }, "data": { "vehicles": [ { "vehicleId": 6, "deviceId": 1, } ] }, "include": { "devices": [ { "deviceId": 1, "serialNumber": "1234567890" } ], "institutions": null, "models": null }, "status": true, "message": "Vehicles retrived successfully.", "responseCode": 200 } – Nirmal Aug 13 '20 at 09:50
  • @John above is my json response based on my class property. Here "institutions" and "models" are null so i don't want to display in json response. – Nirmal Aug 13 '20 at 09:50
  • 1
    Why did you add the ASP.NET tag back after I removed it? The tag's description clearly states that it should not be used for ASP.NET Core questions. Also, you still haven't told us which JSON serializer you're using. – ProgrammingLlama Aug 13 '20 at 11:07

1 Answers1

0

You have to create an empty constructor that initializes those lists the JSON result would be empty lists.

public class Include
    {
        public Include(){
           devices = new List<Devices>();
           institutions = new List<Institutions>();
           cars= new List<Cars>();
        }
        public List<Devices> devices { get; set; }
        public List<Institutions> institutions { get; set; }
        public List<Cars> cars{ get; set; }
    }

EDIT

I've just found this .NET Core: Remove null fields from API JSON response i hope it can help. Otherwhise i suggest you write your own OutputFormatter

TraderMoe
  • 84
  • 5
  • 1
    Serializing an empty list (`"foo : []"`) is not the same as ignoring default values, where the key "foo" will be absent entirely. – CodeCaster Aug 13 '20 at 09:46
  • In this case you could implement your own OutputFormatter using https://learn.microsoft.com/en-us/aspnet/core/web-api/advanced/custom-formatters?view=aspnetcore-3.1 – TraderMoe Aug 13 '20 at 09:50