55

How can I configure serialization of my Web API to use camelCase (starting from lowercase letter) property names instead of PascalCase like it is in C#.

Can I do it globally for the whole project?

Andrei
  • 42,814
  • 35
  • 154
  • 218

5 Answers5

94

If you want to change serialization behavior in Newtonsoft.Json aka JSON.NET, you need to create your settings:

var jsonSerializer = JsonSerializer.Create(new JsonSerializerSettings 
{ 
    ContractResolver = new CamelCasePropertyNamesContractResolver(),
    NullValueHandling = NullValueHandling.Ignore // ignore null values
});

You can also pass these settings into JsonConvert.SerializeObject:

JsonConvert.SerializeObject(objectToSerialize, serializerSettings);

For ASP.NET MVC and Web API. In Global.asax:

protected void Application_Start()
{
   GlobalConfiguration.Configuration
      .Formatters
      .JsonFormatter
      .SerializerSettings
      .ContractResolver = new CamelCasePropertyNamesContractResolver();
}

Exclude null values:

GlobalConfiguration.Configuration
    .Formatters
    .JsonFormatter
    .SerializerSettings
    .NullValueHandling = NullValueHandling.Ignore;

Indicates that null values should not be included in resulting JSON.

ASP.NET Core

ASP.NET Core by default serializes values in camelCase format.

Andrei
  • 42,814
  • 35
  • 154
  • 218
  • mvc6 snippet doesn't work for me on beta6? My `IActionsResult` method returns `Json(listItems)` with PascalCase – fiat Aug 02 '15 at 06:37
  • for beta6, you need `services.AddMvc().Configure(options => { var formatter = options.OutputFormatters.FirstOrDefault(f => f is JsonOutputFormatter) as JsonOutputFormatter; formatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); formatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; });` – Chris Woolum Aug 05 '15 at 04:13
  • so what is the solution to change from { MyProp: ''} to { myProp: ''} – juan carlos peña cabrera Mar 21 '22 at 23:30
6

For MVC 6.0.0-rc1-final

Edit Startup.cs, In the ConfigureServices(IserviceCollection), modify services.AddMvc();

services.AddMvc(options =>
{
    var formatter = new JsonOutputFormatter
    {
        SerializerSettings = {ContractResolver = new CamelCasePropertyNamesContractResolver()}
    };
    options.OutputFormatters.Insert(0, formatter);
});
Jon
  • 9,156
  • 9
  • 56
  • 73
2

ASP.NET CORE 1.0.0 Json serializes have default camelCase. Referee this Announcement

1

If you want to do this in the newer (vNext) C# 6.0, then you have to configure this through MvcOptions in the ConfigureServices method located in the Startup.cs class file.

services.AddMvc().Configure<MvcOptions>(options =>
{
    var jsonOutputFormatter = new JsonOutputFormatter();
    jsonOutputFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    jsonOutputFormatter.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore;

    options.OutputFormatters.Insert(0, jsonOutputFormatter);
});
Vivendi
  • 20,047
  • 25
  • 121
  • 196
0

As others have pointed out camelCase is the default. But if you don't want this everywhere you can annotate your classes like this.

using System.Text.Json.Serialization;

public class Developer
{
    [JsonPropertyName("Id")]
    public int Id { get; set; }
    [JsonPropertyName("FirstName")]
    public string? FirstName { get; set; }
    [JsonPropertyName("LastName")]
    public string? LastName { get; set; }
    [JsonPropertyName("RowNum")]
    public int RowNum { get; set; }
    [JsonPropertyName("StartDate")]
    public DateTime StartDate { get; set; }

}
Norbert Norbertson
  • 2,102
  • 1
  • 16
  • 28