Ignoring null fields in Json.net
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Json.NET can skip properties whose values are null, which often makes outgoing JSON cleaner and smaller. The important design point is that omitting a property is not the same thing as serializing it with an explicit null, so the setting should be chosen based on API semantics, not only on output appearance.
Omit Nulls Globally With NullValueHandling.Ignore
The standard global setting is NullValueHandling.Ignore. When enabled, Json.NET does not write properties whose values are null.
In the output, only Name appears because the other properties are null.
Apply the Rule Per Property When Global Omission Is Too Broad
Sometimes a global rule is too aggressive. Json.NET also lets you control null handling at the property level.
This is useful when one optional field should disappear if null, while the rest of the contract should continue to represent nulls explicitly.
Missing and Null Can Mean Different Things
This is the most important part of the topic. In many APIs:
- a missing property means "not provided"
- an explicit null means "clear this value"
If clients or servers depend on that distinction, switching on global null omission changes contract behavior even though your model classes stay the same. That is why serializer settings belong in API design discussions, not only in formatting preferences.
Configure ASP.NET Carefully
If your application uses Json.NET in ASP.NET, configure the serializer centrally so behavior is consistent.
This only works if the application is actually using the Json.NET pipeline. If the project is serializing with System.Text.Json, these settings will have no effect.
Null References Are Not the Same as Empty Collections
Json.NET skips null references when null omission is enabled, but empty collections still serialize because they are not null.
If ShippingAddress is null, it may disappear. If Notes is an empty list, it still appears as an empty array unless you add different rules. That distinction matters in contract tests.
Treat It as a Contract Choice, Not a Cosmetic Trick
Developers often enable null omission to reduce noise in the JSON. That is a reasonable goal, but it should not be the only consideration. Ask whether the receiving side distinguishes between missing data and explicit null, whether documentation promises one behavior or the other, and whether partial-update endpoints rely on the difference.
Once you think of null omission as a contract choice, the correct configuration usually becomes much clearer.
Common Pitfalls
- Assuming that omitting a property and serializing it as
nullmean the same thing. - Applying global null omission when only one or two fields were supposed to be omitted.
- Debugging Json.NET settings in an app that is actually using
System.Text.Json. - Forgetting that empty collections are still serialized even when null properties are ignored.
- Changing serializer behavior without updating contract tests or client expectations.
Summary
- Json.NET can omit null-valued properties with
NullValueHandling.Ignore. - The rule can be applied globally or at the property level.
- Missing properties and explicit null values often mean different things in APIs.
- Null objects can disappear while empty collections still serialize normally.
- Choose the behavior intentionally as part of the API contract, not just for prettier JSON.

