Ignore null properties during JSON serialization in C#
By FoxLearn 2/5/2025 7:16:39 AM 295
For instance, the following JSON includes a null value for the Description property:
{ "Title": "Inception", "BoxOfficeMillions": 829.89, "Description": null }
However, you can ignore null properties in two main ways:
- Use a serialization setting to ignore all null properties.
- Use an attribute to ignore a specific property when it's null.
In this article, I’ll show examples of how to ignore null properties with both System.Text.Json and Newtonsoft.
Ignore Null Properties with System.Text.Json
To ignore null properties, you can use the JsonIgnoreCondition.WhenWritingNull
option in System.Text.Json
.
Here’s how to apply it in two ways:
For All Properties
To ignore null properties globally, configure the JsonSerializerOptions.DefaultIgnoreCondition
to JsonIgnoreCondition.WhenWritingNull
. This will apply to all properties.
using System.Text.Json; using System.Text.Json.Serialization; var movie = new Movie() { Title = "Inception", BoxOfficeMillions = 829.89, Description = null }; var jsonOptions = new JsonSerializerOptions() { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }; var json = JsonSerializer.Serialize(movie, jsonOptions); Console.WriteLine(json);
Output:
{ "Title": "Inception", "BoxOfficeMillions": 829.89 }
For Specific Properties
If you only want to ignore a specific property when it's null, you can apply the JsonIgnore
attribute with JsonIgnoreCondition.WhenWritingNull
.
using System.Text.Json.Serialization; public class Movie { public string Title { get; set; } public decimal BoxOfficeMillions { get; set; } [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Description { get; set; } }
In this example, only the Description
property is ignored if it is null, while other properties are serialized normally.
WhenWritingNull vs WhenWritingDefault
- WhenWritingNull ignores properties with null values.
- WhenWritingDefault ignores both null properties and properties with their default values (like
0
for integers,false
for booleans).
Here’s an example where WhenWritingDefault
is used:
var book = new Book() { Title = "Atomic Habits", Pages = 0, // int (default value) Author = null // string (null value) }; var jsonOptions = new JsonSerializerOptions() { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault }; var json = JsonSerializer.Serialize(book, jsonOptions); Console.WriteLine(json);
Output:
{ "Title": "Atomic Habits" }
Note that both null properties and properties with default values are omitted.
Ignore Null Properties with Newtonsoft
For those using Newtonsoft.Json, you can use NullValueHandling.Ignore
to ignore null properties. This can be applied globally or on specific properties.
For All Properties
To ignore all null properties globally, use the JsonSerializerSettings.NullValueHandling
setting:
using Newtonsoft.Json; var book = new Book() { Title = "Atomic Habits", Pages = null }; var jsonSettings = new JsonSerializerSettings() { Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore }; var json = JsonConvert.SerializeObject(book, jsonSettings); Console.WriteLine(json);
Output:
{ "Title": "Atomic Habits" }
For Specific Properties
To ignore a specific property when it's null, use the JsonProperty
attribute with NullValueHandling.Ignore
:
using Newtonsoft.Json; public class Book { public string Title { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public int? Pages { get; set; } }
In this case, if Pages
is null, it will not be included in the serialized JSON, but other properties will still be serialized.
- Deserialize JSON using different property names in C#
- Deserialize JSON to a derived type in C#
- Deserialize JSON to a dictionary in C#
- Deserialize a JSON array to a list in C#
- Serialize a tuple to JSON in C#
- Serialize and Deserialize a Multidimensional Array in JSON using C#
- Modifying Date Format for JSON Serialization in C#
- Serialize anonymous types with System.Text.Json in C#