Mastering JSON Serialization in C#: Ignoring Null Properties
When working with JSON in C#, you might encounter situations where you want to ignore null properties during serialization. This can be particularly useful when you're dealing with complex objects that have nullable types, and you want to exclude null values from the resulting JSON. In this article, we'll explore how to achieve this using the `JsonSerializer` class from the `System.Text.Json` namespace, which is the recommended JSON serializer in .NET Core and later versions.
Understanding the Default Behavior
Before diving into how to ignore null properties, let's first understand the default behavior of the `JsonSerializer`. By default, it includes all properties, including null ones, in the serialized JSON output. This can lead to JSON objects with unnecessary null values, which can clutter your data and make it harder to read and process.
Example of Default Behavior
Consider the following C# class:

public class MyClass
{
public string Name { get; set; }
public int? Age { get; set; }
}
When serializing an instance of this class with `Age` set to null, the default behavior will result in the following JSON:
{
"Name": "John Doe",
"Age": null
}
Ignoring Null Properties
To ignore null properties during serialization, you can use the `JsonSerializerOptions` class and its `IgnoreCondition` property. This property allows you specify when to ignore properties. The `JsonIgnoreCondition.Never` enum value is used by default, which means that all properties are included in the serialized output. To ignore null properties, you can set this property to `JsonIgnoreCondition.WhenWritingNull`.
Example of Ignoring Null Properties
Let's modify the previous example to ignore null properties:

var options = new JsonSerializerOptions
{
IgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
var myClass = new MyClass
{
Name = "John Doe",
Age = null
};
string json = JsonSerializer.Serialize(myClass, options);
The resulting JSON will be:
{
"Name": "John Doe"
}
As you can see, the `Age` property, which was null, has been excluded from the JSON output.
Ignoring Specific Null Properties
In some cases, you might want to ignore only specific null properties, while including others. To achieve this, you can use the `[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]` attribute on the properties you want to ignore. This allows you to have fine-grained control over which null properties are excluded.

Example of Ignoring Specific Null Properties
Let's modify the `MyClass` class to ignore only the `Age` property when it's null:
public class MyClass
{
public string Name { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? Age { get; set; }
}
With this modification, only the `Age` property will be excluded from the JSON output when it's null, while other properties will be included as usual.
Ignoring Null Properties in Complex Objects
When dealing with complex objects that have nullable types, you might want to ignore null properties at any level of nesting. To achieve this, you can use the `JsonSerializerOptions.DefaultIgnoreCondition` property and set it to `JsonIgnoreCondition.WhenWritingNull`. This will ensure that all null properties, regardless of their level of nesting, are ignored during serialization.
Example of Ignoring Null Properties in Complex Objects
Consider the following C# class:
public class MyClass
{
public string Name { get; set; }
public MyNestedClass Nested { get; set; }
}
public class MyNestedClass
{
public int? Age { get; set; }
}
When serializing an instance of `MyClass` with `Nested.Age` set to null, using the following `JsonSerializerOptions`:
var options = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
The resulting JSON will be:
{
"Name": "John Doe"
}
As you can see, the `Nested` property and its null `Age` property have been excluded from the JSON output.
Conclusion
In this article, we've explored how to ignore null properties during JSON serialization in C# using the `JsonSerializer` class. We've seen how to ignore null properties in general, how to ignore specific null properties, and how to ignore null properties in complex objects. By mastering these techniques, you can create cleaner, more readable JSON output, and make your code more efficient and maintainable.





















