Prevent null values from being displayed on API JSON response

CrazyCoder

I have a simple DTO object like this:

        var policyDetails = new PolicyDetailsDto
        {
            PolicyId = policy.Id,
            CustomerId = policy.CustomerId,
            AgentDetails = new AgentDetailsDto
            {
                AgencyName = offices?.MarketingName,
                AgencyPhoneNumbers = new List<string> { offices?.DapPhone, offices?.ContactPhone },
                AgentPhoneNumbers = new List<string> { employees?.BusinessPhone, employees?.HomePhone, employees?.MobilePhone }
            }
        };

When I return this dto object from my API to the client, i am getting null values displayed for AgencyPhoneNumbers and AgentPhoneNumbers like this:

{
"policyId": "4185a3b8-4499-ea11-86e9-2818784dcd69",
"customerId": "afb2a6e3-37a4-e911-bcd0-2818787e45b7",
"agentDetails": {
    "agencyName": "ABC Agency",
    "agencyPhoneNumbers": [
        "999-666-4000",
         null
    ],
    "agentPhoneNumbers": [
        "5555555555",
        null,
        null
    ]
}}

This is the AgentDetailsDto class

[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
public class AgentDetailsDto
{
    public string AgencyName { get; set; }
    public List<string> AgencyPhoneNumbers { get; set; }
    public List<string> AgentPhoneNumbers { get; set; }
}

How can I prevent null values from showing up in the list in my JSON response?

Shahid Manzoor Bhat

You can ignore the null values in your WebApiConfig

config.Formatters.JsonFormatter.SerializerSettings = 
                 new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore};

if you are using .NET Core you can use this

public void ConfigureServices(IServiceCollection services)
{
     services.AddMvc()
             .AddJsonOptions(options => {
                options.JsonSerializerOptions.IgnoreNullValues = true;
     });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Prevent JPanel subcomponents from being displayed twice

Prevent a command output from being displayed in a pager

How to parse json values from API response

How can I prevent appsettings.json values from being overridden when running on localhost in .NET 7 Web API?

Null response values from REST API using restTemplate

Prevent dateInput from being NULL in R Shiny

Prevent null values from being emitted in a WebAPI OData v4 service

How do I prevent system information from being displayed on a terminal?

How to prevent empty item in list from being displayed?

How to prevent the block from being displayed, finally, if there is no path to the file

How to prevent commands from being displayed if the extension is not active?

How to use Filter with ReactJS to prevent duplicates in an array from being displayed

Prevent specific plot entry from being displayed on a MATLAB plot legend

How to prevent the previous string from being displayed with typed.js

Deal with NULL values in json response

.NET Core: Remove null fields from API JSON response

Get count of unique values of properties from JSON API response

Null response from API in Angular

Exclude certain values being displayed from a csv file

How to prevent serialization to Json null values?

null values not being removed from list

Prevent datepicker values from being stripped from params

wants to null values after api response is true

Google Charting API stop negative numbers from being displayed

Fetching data from API but no data are being displayed in html

remove null values in json response swift

Why is only the first row is being displayed from JSON list

tastypie queryset values is not being displayed

How to prevent the values of properties initialized by the constructor from being overwritten