C# – Ignore null properties during JSON serialization

By default, null properties are included during JSON serialization like this:

{ "Title": "Top Gun: Maverick", "BoxOfficeMillions": 1340, "Notes": null }
Code language: JSON / JSON with Comments (json)

There are two ways to ignore null properties:

  • Use a serialization setting that makes it ignore all null properties.
  • Use an attribute to ignore a property if it’s null.

In this article, I’ll show examples of these two ways to ignore null properties. I’ll show how to do it with System.Text.Json and Newtonsoft.

Ignore null properties with System.Text.Json

Use JsonIgnoreCondition.WhenWritingNull to ignore null properties. You can apply this to all properties or specific properties, as I’ll show below.

For all properties

To ignore all null properties, set JsonSerializerOptions.DefaultIgnoreCondition to JsonIgnoreCondition.WhenWritingNull. Here’s an example:

using System.Text.Json; using System.Text.Json.Serialization; var movie = new Movie() { Title = "Top Gun: Maverick", BoxOfficeMillions = 1340, Notes = null }; var jsonOptions = new JsonSerializerOptions() { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }; var json = JsonSerializer.Serialize(movie, jsonOptions); Console.WriteLine(json);
Code language: C# (cs)

This outputs the following, showing that it ignored the null Notes property:

{ "Title": "Top Gun: Maverick", "BoxOfficeMillions": 1340 }
Code language: JSON / JSON with Comments (json)

Configure per property

To ignore a specific property if it’s null, use the JsonIgnore attribute with JsonIgnoreCondition.WhenWritingNull, like this:

using System.Text.Json.Serialization; public class Movie { public string Title { get; set; } public decimal BoxOfficeMillions { get; set; } [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Notes { get; set; } }
Code language: C# (cs)

As the name suggests, JsonSerializerOptions.DefaultIgnoreCondition is the default, and you can override this default by using the JsonIgnore attribute. So for example, you could ignore all null properties except for specific ones by overriding the default with JsonIgnoreCondition.Never.

WhenWritingNull vs WhenWritingDefault

If you looked at the JsonIgnoreCondition enum, you may be wondering, what is the difference between WhenWritingNull and WhenWritingDefault?

WhenWritingNull ignores null properties.

WhenWritingDefault ignores null properties AND value type properties set to their default value (0 for int, false for bool, etc…). Here’s an example:

var book = new Book() { Title = "Antifragile", Pages = 0, //int Author = null //string }; var jsonOptions = new JsonSerializerOptions() { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault }; var json = JsonSerializer.Serialize(book, jsonOptions); Console.WriteLine(json);
Code language: C# (cs)

This outputs the following JSON. This shows that it ignored the properties with default values (null and

{ "Title": "Antifragile" }
Code language: JSON / JSON with Comments (json)

And to be clear: nullable value types (i.e. int?) are only ignored if they are null. It doesn’t look at the underlying type’s default value. It works the same with WhenWritingDefault and WhenWritingNull.

I wouldn’t suggest using WhenWritingDefault in most cases. It’s probably only useful in a few scenarios. One possible scenario is when you have optional value type properties and you can’t just make them nullable value types, so you’re using their default values to indicate that they haven’t been set.

Ignore null properties with Newtonsoft

When you’re using Newtonsoft, use NullValueHandling.Ignore to ignore null properties. This can be applied to all properties or specific ones, as I’ll show below.

For all properties

To ignore all null properties, set JsonSerializerSettings.NullValueHandling to NullValueHandling.Ignore. Here’s an example:

using Newtonsoft.Json; var book = new Book() { Title = "Antifragile", Pages = null }; var jsonSettings = new JsonSerializerSettings() { Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore }; var json = JsonConvert.SerializeObject(book, jsonSettings); Console.WriteLine(json);
Code language: C# (cs)

This outputs the following JSON. Notice that it didn’t output the null property:

{ "Title": "Antifragile" }
Code language: JSON / JSON with Comments (json)

Configure per property

To ignore a specific property, use the JsonProperty attribute with NullValueHandling.Ignore, like this:

using Newtonsoft.Json; public class Book { public string Title { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public int? Pages { get; set; } }
Code language: C# (cs)

Note: This attribute overrides whatever you put for JsonSerializerSettings.NullValueHandling.

Leave a Comment