How to Turn Off or Handle Camelcasing in JSON Response ASP.NET Core

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



Leave a reply



Submit