JsonSerializerOptions Ignore when null

Hey guys, I'm writing on an API and we have some values that can be null. I'm looking to hide those out if they are null. What I was doing so far was to use at property level the following: [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] As far as I've seen in stackoverflow and somewhere else is that I could have something like this for applying that to all of the properties:
builder.Services.AddControllers().AddJsonOptions(options => {
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});
builder.Services.AddControllers().AddJsonOptions(options => {
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});
This so far is not hiding the null values for all of the properties This is an example of the response object on the api:
"location": {
"latitude": 40.7128,
"longitude": 74.006,
"address": null,
"city": null,
"country": "US",
"state": null,
"zipCode": null,
"placeType": null,
"isMetricRegion": false
},
"location": {
"latitude": 40.7128,
"longitude": 74.006,
"address": null,
"city": null,
"country": "US",
"state": null,
"zipCode": null,
"placeType": null,
"isMetricRegion": false
},
17 Replies
Pobiega
Pobiega•7mo ago
Hm, need more details. This works fine for me. What .NET version are you using? program.cs
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.DefaultIgnoreCondition =
System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull;
});

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.MapControllers();
app.UseHttpsRedirection();

app.Run();
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.DefaultIgnoreCondition =
System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull;
});

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.MapControllers();
app.UseHttpsRedirection();

app.Run();
controller
using Microsoft.AspNetCore.Mvc;

namespace WebApplication6;

[ApiController]
[Route("api/test")]
public class MyController : ControllerBase
{
[HttpGet]
public TestDto Get()
{
var dto = new TestDto() { Name = "Steve" };

return dto;
}
}

public class TestDto
{
public string? Name { get; set; }
public int? Age { get; set; }
}
using Microsoft.AspNetCore.Mvc;

namespace WebApplication6;

[ApiController]
[Route("api/test")]
public class MyController : ControllerBase
{
[HttpGet]
public TestDto Get()
{
var dto = new TestDto() { Name = "Steve" };

return dto;
}
}

public class TestDto
{
public string? Name { get; set; }
public int? Age { get; set; }
}
this correctly returns
{
"name": "Steve"
}
{
"name": "Steve"
}
for me this is using .net 8, for the record
AdRi🅰nSiT🅾🤔
I'm using .net 6.0
Pobiega
Pobiega•7mo ago
ok I'll try downgrading works fine 🙂 could you perhaps make a minimal reproduction of your codebase with the error in it?
AdRi🅰nSiT🅾🤔
Yeah, give me a sec
AdRi🅰nSiT🅾🤔
Let me know if anything
Pobiega
Pobiega•7mo ago
well your props are not maked as nullable string city is not the same as string? city
AdRi🅰nSiT🅾🤔
but strings can be null?, shouldn't those be skipped?
Pobiega
Pobiega•7mo ago
wdym?
AdRi🅰nSiT🅾🤔
That I would assume that, all fields with null, should be hidden Let me try with the string? instead Still showing with string?
Pobiega
Pobiega•7mo ago
right. but if null is an acceptable value for the prop, it should be string? however, I specifically asked for a minimal reproduction, which isnt a video of the error, its a runnable example of the problem with the smallest possible amount of code where it still shows a github link to the original project would also be good
AdRi🅰nSiT🅾🤔
hmm I don't think I can send that I'll try to make a small example trying to reproduce my issue I cannot share this codebase
Pobiega
Pobiega•7mo ago
great
AdRi🅰nSiT🅾🤔
hmm, trying to replicate it with the weather API works just fine also My code then has to be doing something elsewhere
Pobiega
Pobiega•7mo ago
overriding the formatter anywhere? using newtonsoft json instead of STJ somewhere?
AdRi🅰nSiT🅾🤔
As far as I know we're using the STJ instead, but I could be wrong, I'm not the main codeowner for this It's curious that we have this on some properties [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] and with that they are working But not with the default one
Pobiega
Pobiega•7mo ago
yeah thats curious.
Want results from more Discord servers?
Add your server