Detect if deserialized object is missing a field with the JsonConvert class in Json.NET Newtonsoft
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To detect missing fields during JSON deserialization with Newtonsoft Json.NET, set MissingMemberHandling.Error in JsonSerializerSettings to throw when the JSON contains fields not in your class, or use [JsonProperty(Required = Required.Always)] to throw when your class expects a field that is missing from the JSON. You can also use JsonSerializer error handling events to log missing fields without throwing. These mechanisms ensure data integrity by catching schema mismatches early.
Detect Missing Fields in JSON (Required Properties)
When your C# class requires a field but the JSON does not include it:
Required Attribute Options
Detect Extra Fields in JSON (MissingMemberHandling)
When the JSON contains fields not in your C# class:
By default, MissingMemberHandling is set to Ignore, meaning extra JSON fields are silently skipped.
Using Error Handling Events
Log missing fields without throwing:
Attribute-Level Error Handling
Validating After Deserialization
Check for null fields after deserialization:
Using JsonSchema Validation
For comprehensive schema validation:
Comparing with System.Text.Json
Common Pitfalls
- Confusing
MissingMemberHandlingwithRequired:MissingMemberHandling.Errordetects extra fields in JSON that are not in the C# class.Required.Alwaysdetects fields missing from JSON that are expected by the C# class. These solve opposite problems — use both for strict bi-directional validation. - Not handling
JsonSerializationException: WhenRequired.Alwaysfails, it throwsJsonSerializationException. If unhandled, this crashes the application. Wrap deserialization in try-catch and return meaningful error messages to the caller. - Using
Required.Alwayson value types: ARequired.Alwaysonint Agethrows whenAgeis missing from JSON. Butintdefaults to0, which is a valid value. Useint? Age(nullable) withRequired.AllowNullto distinguish between "missing" (null) and "present with value 0". - Global
MissingMemberHandling.Errorbreaking polymorphic deserialization: If you setMissingMemberHandling.Errorglobally, JSON containing type discriminators or extra fields for derived types throws errors. Apply it per-class with[JsonObject(MissingMemberHandling = ...)]instead of globally. - Assuming default values mean the field was present: After deserialization,
Age == 0could mean the JSON had"Age": 0or thatAgewas missing and defaulted. To distinguish, useint? Age—nullmeans missing,0means explicitly set.
Summary
- Use
[JsonProperty(Required = Required.Always)]to ensure required JSON fields are present - Use
MissingMemberHandling.Errorin settings to detect unexpected extra fields in JSON - Use error handling events (
settings.Error) to log issues without throwing exceptions - Combine
Requiredattributes with nullable types (int?) to distinguish missing from default values - For comprehensive validation, use
Newtonsoft.Json.Schemawith a JSON Schema definition
Related reading
- Detect Windows version in .NET
- Detected package downgrade warning dotnet core, vs 2017
- Detecting async client disconnect in ASP.NET MVC
- Detecting USB drive insertion and removal using windows service and c
- Determine if ASP.NET application is running locally
- Determine .NET Framework version for dll
- Determine via C whether a string is a valid file path
- Determine what control the ContextMenuStrip was used on

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.