Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

2.2K
Views
How to ignore empty list when serializing to json?

I am trying to figure out how to serialize to a json object and skip serializing properties whose values are empty lists. I am not using Newtonsoft json

using System.Text.Json;
using System.Text.Json.Serialization;
using AutoMapper;

I have an object with a property.

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("extension")]
public List<Extension> Extension { get; set; }

When I try to serialize this object using the following

var optionsJson =   new JsonSerializerOptions
    {
    WriteIndented = true,
    IgnoreNullValues = true,
    PropertyNameCaseInsensitive = true,
    };

var json = JsonSerializer.Serialize(report, optionsJson);

It still gives me an empty array:

"extension": [],

Isn't there a way to keep it from serializing these empty lists? I would like to see extension gone. It should not be there at all. I need to do this because the gateway will respond with an error if I send:

"extension": null,

It must not be part of the object when serialized.

gateway error

The reason why I do not want these empty lists is that the third party gateway I am sending to objects to empty lists

"severity": "error", "code": "processing", "diagnostics": "Array cannot be empty - the property should not be present if it has no values", "location": [ "Bundle.entry[2].resource.extension", "Line 96, Col 23" ]

I'm trying to avoid doing some kind of nasty string replace on this.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can add a dummy property that is used during serialization that handles this.

  • Add a new property with the same signature, but flag it with JsonPropertyNameAttribute to ensure it is being serialized with the correct name, and also with the JsonIgnoreAttribute so that it will not be serialized when it returns null.
  • The original property you mark with JsonIgnore, unconditionally, so that it will never be serialized itself
  • This dummy property would return null (and thus be ignored) when the actual property contains an empty list, otherwise it would return that (non-empty) list
  • Writes to the dummy property just writes to the actual property

Something like this:

[JsonIgnore]
public List<Extension> Extensions { get; set; } = new();

[JsonPropertyName("extension")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
 public List<Extension> SerializationExtensions
    {
        get => Extensions?.Count > 0 ? Extensions : null;
        set => Extensions = value ?? new();
    }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!