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();
}Now, when the API receives JSON containing additional properties, the serializer stores those properties inside AdditionalData.
For example:
{
"name": "John",
"email": "[email protected]",
"password": "StrongPassword123",
"deviceName": "iPhone",
"appVersion": "5.2.0"
}The recognized properties are mapped to their respective C# properties, while deviceName and appVersion are stored in AdditionalData.
Developers can access these values as follows:
foreach (var item in user.AdditionalData)
{
Console.WriteLine($"{item.Key}: {item.Value}");
}JsonElement is useful because it allows the application to inspect different JSON value types without immediately converting everything into strings.
For example:
if (user.AdditionalData.TryGetValue("appVersion", out var version))
{
string appVersion = version.GetString();
Console.WriteLine(appVersion);
}This approach is particularly useful when building APIs that integrate with external services whose JSON structures may change over time.
However, additional properties should not automatically be trusted. Validate their names, types, sizes, and allowed values before using them in business logic.
Comparison of Unknown Property Handling Approaches
Different applications require different approaches to unknown JSON properties.
Approach | Behavior | Suitable Scenario |
|---|---|---|
Skip | Ignores unknown properties | Public APIs and mobile applications |
Disallow | Rejects unknown properties | Strict validation and configuration APIs |
JsonExtensionData | Captures additional properties | Flexible integrations and extensible models |
The choice depends on the application's compatibility requirements and how much control it needs over incoming data.
For example, a public API used by multiple mobile application versions may benefit from Skip. An internal API that processes sensitive configuration changes may benefit from Disallow.
JsonExtensionData is useful when additional information must be preserved instead of discarded.
Best Practices for Handling Unknown JSON Fields
Choose the Right Deserialization Strategy
Do not apply the same unknown-property handling strategy to every API without considering its purpose.
Public APIs often need backward compatibility because different clients may update at different times. Internal APIs may have stricter requirements because their clients are controlled by the same development team.
Select the behavior according to the API contract.
Validate Additional Properties
When using JsonExtensionData, validate every additional property before processing it.
For example, do not assume that appVersion will always contain a valid string. A client could send a number, object, array, or null value instead.
Check the JSON value kind before reading it.
if (user.AdditionalData.TryGetValue("appVersion", out var version))
{
if (version.ValueKind == JsonValueKind.String)
{
var appVersion = version.GetString();
// Process the validated value.
}
}This prevents unexpected JSON value types from causing runtime errors.
Avoid Exposing Sensitive Information
Unknown properties may contain passwords, access tokens, personal information, or other sensitive values.
Avoid logging the complete request body or all extension data without reviewing its contents.
Use structured logging and explicitly exclude sensitive fields.
Define Clear API Contracts
Ignoring unknown fields should not become an excuse for unclear API contracts.
Document which fields are required, which are optional, and which additional fields are supported.
When introducing new properties, consider whether older clients can continue working without them.
Test Different JSON Structures
Include tests for requests containing additional properties, missing properties, null values, incorrect data types, and unexpected nested objects.
These tests help identify serialization problems before they reach production.
Common Mistakes to Avoid
One common mistake is assuming that unknown properties are automatically stored in the C# model. By default, they are ignored unless JsonExtensionData is configured.
Another mistake is enabling Disallow globally without checking existing client integrations. This can break requests from older or newer clients that send additional fields.
Developers also sometimes treat captured extension data as trusted input. JsonExtensionData only captures values; it does not validate them.
Finally, avoid using dynamic or loosely typed objects everywhere just to support changing JSON structures. Strongly typed request models provide better maintainability, validation, and readability.
Troubleshooting Common Issues
Problem | Possible Reason | Solution |
|---|---|---|
Unknown properties are missing | Default serializer behavior ignores them | Use JsonExtensionData |
Requests fail after adding Disallow | Incoming JSON contains unmapped properties | Review the request or adjust the handling strategy |
Property names are not matching | Different JSON property casing | Configure PropertyNameCaseInsensitive |
Invalid JSON value causes an exception | Incorrect value type or malformed JSON | Validate the JSON structure and handle JsonException |
Additional data contains unexpected values | Extension data is not validated | Check JsonElement.ValueKind before processing |
When troubleshooting, inspect the actual incoming JSON payload and the serializer configuration used by the API. This helps identify whether the problem comes from property mapping, validation, or deserialization settings.
Advantages and Disadvantages
Advantages
Ignoring unknown properties helps maintain compatibility between different client versions.
Strict handling prevents unexpected fields from silently entering the application.
JsonExtensionData allows APIs to preserve additional information without changing the main model.
System.Text.Json provides built-in configuration options without requiring a separate JSON library.
Developers can choose different handling strategies based on application requirements.
Disadvantages
Ignoring unknown properties can hide client-side mistakes.
Rejecting unknown properties may break integrations when request structures change.
Capturing additional properties requires extra validation and processing.
Large or deeply nested JSON structures can increase memory usage.
Flexible request models can become difficult to maintain if additional properties are not properly documented.
Summary
Handling unknown JSON fields is an important part of building reliable ASP.NET Core APIs. As applications evolve, clients and external services may send properties that are not included in existing C# models.
System.Text.Json provides three useful approaches: Skip, Disallow, and JsonExtensionData. Skip allows the API to ignore additional fields, Disallow rejects unexpected properties, and JsonExtensionData captures additional information for further processing.
The right approach depends on the API's compatibility, validation, and security requirements. For public APIs that support multiple client versions, ignoring unknown fields can provide flexibility. For strict internal operations, rejecting unexpected properties may offer better control. When additional information must be preserved, JsonExtensionData provides a practical solution.
By choosing the correct deserialization strategy, validating incoming data, and testing different JSON structures, developers can build ASP.NET Core APIs that handle changing client requirements without introducing unnecessary compatibility or security problems.

Join the conversation! Your thoughts help the community grow.