Reputation: 39524
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?
Upvotes: 65
Views: 67942
Reputation: 5037
While in .NET 5 and later, set JsonSerializerOptions.DefaultIgnoreCondition
to JsonIgnoreCondition.WhenWritingNull
:
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});
I'm looking at .Net Core 3.1, where this should ignore null values
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.IgnoreNullValues = true;
});
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
Upvotes: 65
Reputation: 8814
For Minimal APIs running in DOTNET 7+ (probably 6 too) you can configure it globally simply as the Minimal API Tutorial's section Configure JSON Serialization Options shows. Just use Builder.Services.ConfigureHttpJsonOptions()
as per the excerpt bellow:
var builder = WebApplication.CreateBuilder(args);
builder.Services.ConfigureHttpJsonOptions(options => {
options.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
// set other desired options here...
options.SerializerOptions.WriteIndented = true;
options.SerializerOptions.IncludeFields = true;
});
var app = builder.Build();
Upvotes: 1
Reputation: 49
If your using .Net core 6 and System.Text.Json, then add this change in program.cs
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
// if you want to remove $Id (metadata) from the response, add the below line
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});
Upvotes: 0
Reputation: 3645
TL&DR: In DotNet 6 NullValueHandling has been deprecated. Instead, use DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
services.AddControllersWithViews().AddJsonOptions(options =>
{
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});
Upvotes: 0
Reputation: 2998
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;
});
Upvotes: 3
Reputation: 3141
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
Upvotes: 60
Reputation: 5421
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;
});
Upvotes: 9
Reputation: 1778
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.
Upvotes: 13
Reputation: 7906
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; });
Upvotes: 10
Reputation: 148
See the official migration guide Migrate from ASP.NET Core 2.2 to 3.0
Your service codes should look like this:
services.AddMvc(c =>
{
})
.AddNewtonsoftJson(
options =>
{
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
options.SerializerSettings.StringEscapeHandling = StringEscapeHandling.EscapeHtml;
options.SerializerSettings.Error = (object sender, ErrorEventArgs args) =>
{
// handle error
};
}
);
Upvotes: -3