ASP.NET Core APIs often receive JSON requests from different clients, including web applications, mobile applications, desktop software, and third-party integrations. As these clients evolve, their JSON payloads may contain properties that the API does not recognize.
For example, imagine an ASP.NET Core API that accepts customer registration requests. The original request model contains customer name, email address, and phone number. Later, a mobile application introduces an additional property called PreferredLanguage.
The older API may not have this property in its request model. Depending on the JSON configuration and deserialization behavior, the API may ignore the additional field, reject the request, or preserve it for further processing.
Handling unknown JSON properties correctly is important because API clients do not always upgrade at the same time. Mobile applications can remain installed for months, third-party integrations may follow different release schedules, and older clients may continue sending payloads that do not match the latest API contract.
In this article, we will explore how ASP.NET Core handles unknown JSON fields, how to configure System.Text.Json safely, how to capture additional properties when required, and how to maintain compatibility between different API clients.
Understanding Unknown JSON Fields
An unknown JSON field is a property included in a request payload that does not exist in the corresponding C# model.
Consider the following request:
{
"name": "Rahul",
"email": "[email protected]",
"phone": "9876543210",
"preferredLanguage": "English"
}
Now consider the corresponding C# model:
public class CustomerRequest
{
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Phone { get; set; } = string.Empty;
}
The API model does not contain PreferredLanguage.
In modern ASP.NET Core applications using System.Text.Json, unknown properties are ignored by default during ordinary deserialization.
This means the request can still be deserialized successfully, but the additional property is not automatically stored in the CustomerRequest object.
That behavior is useful for maintaining compatibility, but developers should understand when ignoring unknown properties is appropriate and when it creates a problem.
Why Unknown Fields Appear in API Requests
There are several common reasons:
- A mobile application introduces a new field before the backend is updated.
- A third-party client sends additional metadata.
- Different application versions use different request models.
- An integration sends properties that are not relevant to the current API.
- A client accidentally uses an incorrect property name.
- A new API feature introduces optional request properties.
Not every unknown property represents an error. However, silently accepting unexpected fields without monitoring them can make integration problems difficult to identify.
How ASP.NET Core Handles Unknown JSON Properties
ASP.NET Core uses input formatters to convert incoming request bodies into C# objects.
For JSON requests, System.Text.Json is commonly used by default.
Consider a simple API controller:
[ApiController]
[Route("api/customers")]
public class CustomersController : ControllerBase
{
[HttpPost]
public IActionResult CreateCustomer(
[FromBody] CustomerRequest request)
{
return Ok(new
{
request.Name,
request.Email,
request.Phone
});
}
}
If the client sends an additional property called PreferredLanguage, the API normally ignores that property during deserialization.
The important distinction is that unknown properties and invalid known properties are different situations.
For example:
{
"name": "Rahul",
"email": "[email protected]",
"phone": 9876543210
}
If Phone is defined as a string, the numeric JSON value may not be accepted as a valid string by the default serializer configuration.
Ignoring unknown fields does not mean accepting every invalid JSON value.
Developers should validate known properties separately from the handling of additional properties.
Configuring Unknown JSON Property Handling
System.Text.Json provides configuration options that control how unknown JSON properties are handled.
Ignoring Unknown Properties
For applications that need to support older and newer clients, ignoring unknown fields is often a reasonable default.
The following configuration explicitly enables this behavior:
builder.Services
.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions
.UnmappedMemberHandling =
JsonUnmappedMemberHandling.Skip;
});
The UnmappedMemberHandling property controls what happens when the JSON payload contains a property that cannot be mapped to the target .NET type.
The Skip option tells the serializer to ignore those properties.
This can be useful when an API supports multiple client versions and does not need to process every property sent by each client.
However, ignoring unknown properties should not replace request validation. The API must still validate required fields, data types, and business rules.
Rejecting Unknown Properties
Some APIs require strict request contracts.
For example, a financial API may accept only explicitly defined request properties. Unexpected fields may indicate an incorrect integration or a client-side implementation problem.
In such cases, System.Text.Json supports rejecting unmapped properties:
builder.Services
.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions
.UnmappedMemberHandling =
JsonUnmappedMemberHandling.Disallow;
});
With this configuration, the serializer throws a JsonException when it encounters an unknown property.
ASP.NET Core handles the deserialization failure through its normal request-processing pipeline. With the API controller behavior, an invalid request body is generally returned as a client error.
This approach is useful when the API contract must remain strict, but it can create compatibility problems if older and newer clients send different payloads.
Comparing Skip and Disallow
| Behavior |
Skip |
Disallow |
| Unknown property |
Ignored |
Rejected |
| Client compatibility |
More flexible |
More restrictive |
| Strict contract enforcement |
Limited |
Strong |
| Suitable for evolving mobile clients |
Often suitable |
Requires careful version management |
| Unexpected property detection |
Not automatic |
Detected during deserialization |
The correct option depends on the API's purpose and compatibility requirements.
Capturing Unknown Properties Instead of Ignoring Them
Sometimes ignoring unknown fields is not enough.
Consider an API that receives metadata from different external systems. Each integration may send additional properties that the API does not know in advance.
Instead of rejecting or discarding those properties, the application may need to preserve them.
System.Text.Json provides the JsonExtensionData attribute for this purpose.
Using JsonExtensionData
Update the request model:
using System.Text.Json;
using System.Text.Json.Serialization;
public class CustomerRequest
{
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Phone { get; set; } = string.Empty;
[JsonExtensionData]
public Dictionary<string, JsonElement>? AdditionalData
{
get;
set;
}
}
Now, if the API receives the following request:
{
"name": "Rahul",
"email": "[email protected]",
"phone": "9876543210",
"preferredLanguage": "English",
"source": "MobileApp",
"campaignId": 105
}
The known properties are mapped to their respective C# properties.
The remaining properties are stored in AdditionalData.
The controller can inspect them:
[HttpPost]
public IActionResult CreateCustomer(
[FromBody] CustomerRequest request)
{
if (request.AdditionalData != null)
{
foreach (var item in request.AdditionalData)
{
Console.WriteLine(
$"Additional field: {item.Key}");
}
}
return Ok(new
{
Message = "Customer request received",
request.Name
});
}
This approach is useful when an API must preserve additional metadata without changing its model every time an integration introduces a new property.
However, captured properties should not automatically become trusted application data.
Validate property names, expected types, value sizes, and permitted usage before storing or processing them.
Also remember that JsonExtensionData changes how unknown properties are handled for that model. Do not combine it with a global strict unmapped-member policy without verifying the behavior you want for your specific model and .NET version.
Handling Unknown Fields With Different API Requirements
Not every API endpoint needs the same unknown-property behavior.
For example, a public search endpoint may accept additional optional filters, while a payment endpoint may require a strict request contract.
Applying one global JSON configuration to every endpoint may not always be appropriate.
A better design is to define request models according to their individual requirements.
For flexible endpoints, use extension data when additional information must be preserved.
For strict endpoints, use explicit validation or a dedicated deserialization configuration.
For endpoints where additional properties are irrelevant, use the default skip behavior.
This keeps API behavior predictable without forcing every client to follow the same evolution strategy.
Best Practices for Production ASP.NET Core APIs
Maintain Backward Compatibility
Mobile applications and external integrations may not upgrade immediately after an API deployment.
Avoid rejecting additional fields unless strict contract enforcement is an actual requirement.
For long-lived clients, maintain compatibility through carefully designed optional properties and versioned contracts.
Validate Known Properties
Ignoring unknown properties does not guarantee that known properties contain valid values.
Use data annotations or dedicated validation logic.
using System.ComponentModel.DataAnnotations;
public class CustomerRequest
{
[Required]
[StringLength(100)]
public string Name { get; set; } = string.Empty;
[Required]
[EmailAddress]
public string Email { get; set; } = string.Empty;
}
These validations help prevent invalid application data from entering the business layer.
Avoid Blindly Persisting Additional Data
Unknown properties may contain sensitive information, unexpected content, or values that are not relevant to the application.
Before persisting extension data, apply appropriate validation and data protection rules.
Monitor Unexpected Properties
If unknown fields are frequently appearing, investigate their source.
Structured logging can help identify client versions and integration problems without exposing sensitive request values.
Avoid logging complete request bodies by default.
Use Explicit API Contracts
Do not depend entirely on serializer behavior to maintain API compatibility.
Document which fields are required, optional, deprecated, or supported for each API version.
Limit Request Payload Size
Even when unknown fields are ignored, the complete request still needs to be received and parsed.
Configure appropriate request-size limits and validate nested JSON structures where necessary.
This helps protect the API from unnecessarily large or abusive payloads.
Common Mistakes When Handling Unknown JSON Fields
| Mistake |
Why It Causes Problems |
Recommended Approach |
| Rejecting every unknown property |
Breaks compatibility with newer clients |
Use Skip when flexibility is required |
| Ignoring every unknown property |
Can hide integration errors |
Monitor unexpected fields |
| Treating unknown fields as trusted data |
May introduce security risks |
Validate before processing |
| Confusing unknown and invalid fields |
Incorrectly assumes all malformed values are ignored |
Validate known properties separately |
| Applying strict handling globally |
Can break unrelated endpoints |
Define endpoint-specific contracts |
| Storing all extension data without limits |
May increase storage and security risks |
Validate size and permitted properties |
Troubleshooting Common JSON Deserialization Problems
API Returns a Bad Request for an Additional Property
Check whether UnmappedMemberHandling is configured as Disallow.
Also inspect any custom converters or model-specific deserialization logic that may reject additional properties.
Additional Properties Are Missing From the Request Model
If the application uses the default behavior, unknown properties are ignored.
Add JsonExtensionData when the application needs to preserve those properties.
Valid JSON Is Still Rejected
Check whether the JSON data types match the C# model.
For example, a number sent for a string property or an incorrect date format may cause a deserialization error even when unknown properties are allowed.
Different Endpoints Behave Differently
Review global JsonOptions, model-specific attributes, custom converters, and any manually configured serializer instances.
Ensure the same endpoint contract is not being processed with conflicting serialization settings.
Summary
Handling unknown JSON fields safely is an important part of building maintainable ASP.NET Core APIs. As applications evolve, different clients may send additional properties that are not present in the current server-side models.
System.Text.Json provides several ways to control this behavior. The Skip option allows the API to ignore unknown properties, while Disallow enforces stricter request contracts. JsonExtensionData provides another option when additional fields need to be captured and processed.
For APIs supporting mobile applications and third-party integrations, flexible handling is often useful for maintaining backward compatibility. However, flexibility should not mean accepting unvalidated data.
The best approach is to define clear API contracts, validate known properties, monitor unexpected fields, and apply strict handling only where the business requirements justify it. These practices help ASP.NET Core APIs remain secure, predictable, and compatible as different clients continue to evolve.
Join the conversation! Your thoughts help the community grow.