Introduction
JSON is one of the most common formats used for exchanging data between applications. ASP.NET Core APIs use JSON to receive requests from clients, return responses, and communicate with external services. In a typical application, the backend defines a model that represents the expected JSON structure, and the framework automatically converts incoming JSON into C# objects.
However, real-world applications do not always receive JSON containing only the properties defined in their models. A mobile application may send additional fields after an update, a third-party service may introduce new properties, or an older client may communicate with a newer API version.
These situations introduce an important question: what should an ASP.NET Core API do when it receives JSON fields that are not defined in its C# model?
Ignoring unknown fields can make an API easier to maintain when clients evolve independently. However, accepting unexpected data without understanding the behavior can also introduce security, validation, and compatibility problems.
In this article, we will explore how ASP.NET Core handles unknown JSON fields, how to configure JSON deserialization, how to capture additional properties when required, and how to build APIs that remain reliable when JSON structures change.
Understanding Unknown JSON Fields
Consider an ASP.NET Core API that accepts user registration requests. The application defines the following model:
public class RegisterUserRequest
{
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}The API expects JSON similar to the following:
{
"name": "John",
"email": "[email protected]",
"password": "StrongPassword123"
}Now imagine that a mobile application sends an additional property:
{
"name": "John",
"email": "[email protected]",
"password": "StrongPassword123",
"deviceName": "iPhone",
"appVersion": "5.2.0"
}The properties deviceName and appVersion do not exist in the RegisterUserRequest class.
By default, System.Text.Json ignores unmapped JSON properties during deserialization. This means the API can still deserialize the request into the existing C# model without requiring changes to the model.
This behavior is useful when multiple client versions communicate with the same backend. However, developers should understand that ignoring a property does not mean the API validates or processes its value.
How ASP.NET Core Handles Unknown JSON Properties
ASP.NET Core uses System.Text.Json by default for JSON request and response processing in modern Web API applications.
When an incoming JSON document is converted into a C# object, the serializer matches JSON properties with the corresponding properties in the target class.
For example:
var json = """
{
"name": "John",
"email": "[email protected]",
"password": "StrongPassword123",
"department": "Engineering"
}
""";
var user = JsonSerializer.Deserialize<RegisterUserRequest>(json);
Console.WriteLine(user.Name);
Console.WriteLine(user.Email);The serializer maps the recognized properties and ignores department because it is not defined in the model.
This default behavior is generally suitable for APIs that need to support different client versions without breaking existing integrations.
However, there are situations where silently ignoring unknown properties is not appropriate. For example, an API that processes financial transactions or sensitive configuration data may need strict control over every incoming field.
Configuring Unknown Property Handling
System.Text.Json provides configuration options that allow developers to control how unknown JSON properties are handled.
Allow Unknown Properties
The default behavior allows unknown properties during deserialization.
Developers can also explicitly configure this behavior using JsonSerializerOptions.
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip
};
var user = JsonSerializer.Deserialize<RegisterUserRequest>(
json,
options
);JsonUnmappedMemberHandling.Skip tells the serializer to ignore JSON properties that do not match members in the target C# model.
PropertyNameCaseInsensitive allows the serializer to match property names without considering uppercase and lowercase differences.
This configuration is useful when an application integrates with clients that may send additional properties or use slightly different property casing.
Reject Unknown Properties
For APIs that require strict request validation, unknown properties can be rejected instead of ignored.
System.Text.Json provides JsonUnmappedMemberHandling.Disallow for this purpose.
var options = new JsonSerializerOptions
{
UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow
};
try
{
var user = JsonSerializer.Deserialize<RegisterUserRequest>(
json,
options
);
}
catch (JsonException ex)
{
Console.WriteLine($"Invalid JSON request: {ex.Message}");
}When the JSON contains an unmapped property, deserialization throws a JsonException.
This approach is useful for APIs that accept sensitive configuration data, financial instructions, or requests where unexpected fields should never be accepted.
It is important to remember that rejecting unknown properties can affect backward compatibility. If a newer mobile application starts sending additional fields to an older backend, the request may fail.
Configuring Unknown Properties Globally in ASP.NET Core
In an ASP.NET Core application, developers can configure JSON serialization behavior globally through the controller services.
For example, consider the following configuration in Program.cs:
using System.Text.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
options.JsonSerializerOptions.UnmappedMemberHandling =
JsonUnmappedMemberHandling.Skip;
});
var app = builder.Build();
app.MapControllers();
app.Run();This configuration applies the selected JSON behavior to controller-based APIs.
If the application should reject unknown properties, change Skip to Disallow.
For example:
options.JsonSerializerOptions.UnmappedMemberHandling =
JsonUnmappedMemberHandling.Disallow;Global configuration should be selected carefully because it affects multiple API endpoints. A strict configuration that works well for administrative APIs may create unnecessary compatibility problems for public mobile APIs.
Capturing Unknown JSON Properties
Sometimes ignoring unknown properties is not enough. An application may need to preserve additional fields for logging, forwarding data to another service, or supporting flexible integrations.
System.Text.Json provides the JsonExtensionData attribute for this purpose.
Consider the following model:
using System.Text.Json;
using System.Text.Json.Serialization;
public class RegisterUserRequest
{
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
[JsonExtensionData]
public Dictionary<string, JsonElement> AdditionalData { get; set; }
= new();
}
Arun PottiPosted Nov 26, 2026, 10:57 PM
Good Article.