AdRi🅰nSiT🅾🤔
AdRi🅰nSiT🅾🤔
CC#
Created by AdRi🅰nSiT🅾🤔 on 5/14/2024 in #help
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
},
35 replies