Lowercase JSON with ASP.NET Web API

Written on April 05, 2012

When using JSON (especially when consuming it from JavaScript) it's common to use lower case.

When returning JSON using ASP.NET Web API, the casing of the JSON generated by the formatters depends on the casing of your DTOs (you're using DTOs, right?!) you're returning using Web API.

In order to get always lower case JSON, the simplest way is to use the new JsonMediaTypeFormatter in ASP.NET Web API or the JsonNetFormatter from the WebApiContrib project both of which use Newtonsoft.Json to (de-)serialize JSON .

Independent of which you're using, you can create lowercase JSON by providing a LowercaseContractResolver which is injected to the JSON serialization process:

When using the official JsonMediaTypeFormatter, you can pass in the SerializerSettings this way:

When using the JsonNetFormatter from WebApiContrib, you can pass in the SerializerSettings this way:

The LowerCaseContractResolver has been posted by alexn on SO.