'Ignore property when null using the new Net Core 3.0 Json

When using JSON.Net in ASP.Net Core 2.2 I was able to ignore a property when its value was null when serializing to JSON:

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public DateTime? Created { get; set; }

But when using the new ASP.Net Core 3.0 built in JSON (System.Text.Json) I can’t find an equivalent attribute to ignore a property if its value is null.

I could only find JsonIgnore.

Am I missing something?



Solution 1:[1]

This was fixed on .Net 5

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]

See the updates below

https://github.com/dotnet/runtime/issues/41313

https://github.com/dotnet/runtime/issues/30687

Solution 2:[2]

I'm looking at .Net Core 3.1, where this should ignore null values

services.AddControllers().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.IgnoreNullValues = true;
});

While in .NET 5 and later, set JsonSerializerOptions.DefaultIgnoreCondition to JsonIgnoreCondition.WhenWritingNull:

services.AddControllers().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});

Note, the above isn't per property/attribute, although there is an attribute which may be helpful JsonIgnoreAttribute.

An alternative, to solve your problem might be a JsonConverterAttribute, information on how to write your own converter is here

Solution 3:[3]

If you want property level control of ignoring null values during JSON serialization, for Net Core 3.1 you'll have to write a custom converter. There are examples described in the Newtonsoft.Json migration documentation.

That is a big hassle for functionality that was declarative using Newtonsoft.Json. You can specify to use Newtonsoft.Json by specifying as much in Startup.ConfigureServices().

services.AddControllers()
    .AddNewtonsoftJson();

As the documentation notes, you'll need to add the Microsoft.AspNetCore.Mvc.NewtonsoftJson package.

Solution 4:[4]

Adding this to your startup should help although it's not per property and it's not an attribute.

services.AddMvc()
        .AddJsonOptions(options =>{ options.JsonSerializerOptions.IgnoreNullValues = true; });

Solution 5:[5]

If you are still using Newtonsoft.Json in .net core 3.1, you want to have configuration like below.

services
   .AddControllers()
   .AddJsonOptions(options =>
   {
       options.JsonSerializerOptions.IgnoreNullValues = true;
   })
   .AddNewtonsoftJson(options =>
   {
       options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
   });

Solution 6:[6]

If you are using System.Text.Json (.net 5.0) and you want to ignore all null use WhenWritingNull condition:

services.AddControllers().AddJsonOptions(a =>
     {
        a.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;            
        a.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
     });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Murilo Maciel Curti
Solution 2 dbc
Solution 3 dblood
Solution 4 Moriya
Solution 5 cdev
Solution 6 Daniel