How to turn off or handle camelCasing in JSON response ASP.NET Core?
In ASP.NET Core <3.0, JSON properties are camelCased by default (per this announcement).
You can disable this by replacing
services.AddMvc();
with
services
.AddMvc()
.AddJsonOptions(opt => opt.SerializerSettings.ContractResolver
= new DefaultContractResolver());
in your Startup.cs file. You'll have to add using Newtonsoft.Json.Serialization;
to the top of the file.
With the DefaultContractResolver
in place, the property names will be represented verbatim in the JSON output. No need for DataMember
attributes.
How to stop newtonsoft json from camelcasing
In your startup's ConfigureServices
you can add MvcJsonOptions
.
services
.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver =
new Newtonsoft.Json.Serialization.DefaultContractResolver();
});
asp.net core 1.0 web api use camelcase
services
.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver
= new Newtonsoft.Json.Serialization.DefaultContractResolver();
});
This keeps a JSON object's name the same as .NET class property.
return JSON as is (PascalCase) from one Action
The Json
method is part of Controller
, but isn't part of ControllerBase
. If you're using ControllerBase
, which is typical for controllers that don't use views, you can new up a JsonResult
and return that:
return new JsonResult(myObj, cfgHere);
This is all the Controller.Json
method really does, as can be seen in the source:
public virtual JsonResult Json(object data, object serializerSettings)
{
return new JsonResult(data, serializerSettings);
}
serializerSettings
can be either JsonSerializerOptions
or JsonSerializerSettings
(if you're using Json.NET). Here's an example that assumes you're using the default, System.Text.Json
-based formatters:
return new JsonResult(myObj, new JsonSerializerOptions());
By creating an instance of JsonSerializerOptions
without setting any properties, the PropertyNamingPolicy
is left as the default policy, which leaves the property names as-is.
If you'd like to use a more declarative approach, which supports content-negotiation, see: Change the JSON serialization settings of a single ASP.NET Core controller.
ASP.NET Core 3.0 System.Text.Json Camel Case Serialization
AddJsonOptions()
would config System.Text.Json
only for MVC. If you want to use JsonSerializer
in your own code you should pass the config to it.
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
var json = "{\"firstname\":\"John\",\"lastname\":\"Smith\"}";
var person = JsonSerializer.Parse<Person>(json, options);
Web API 2: how to return JSON with camelCased property names, on objects and their sub-objects
Putting it all together you get...
protected void Application_Start()
{
HttpConfiguration config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;
}
.NET 6 - AddJsonOptions with CamelCase not working
JsonSerializer.Deserialize
does not use JsonSerializerOptions
which are configured by AddJsonOptions
, create and pass required options manually (possibly resolve ones from the DI via JsonOptions
):
var result = JsonSerializer.Deserialize<InvestimentFundsResponseData>(content, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters = {new JsonStringEnumConverter()},
IgnoreNullValues = true
});
Related Topics
What's the Best Way to Calculate the Size of a Directory in .Net
How to Check If Any Flags of a Flag Combination Are Set
ASP.NET Identity Get All Roles of Logged in User
Return JSON with Error Status Code MVC
How to Check Whether an Object Has Certain Method/Property
What Does the Keyword "New" Do to a Struct in C#
What's the Difference Between Anonymous Methods (C# 2.0) and Lambda Expressions (C# 3.0)
Catch All Unhandled Exceptions in ASP.NET Web API
Adding Stored Procedures Complex Types in Entity Framework
Displayname Attribute VS Display Attribute
How to Make Method Call Another One in Classes
Can You Have a Property Name Containing a Dash