Returning camelCase JSON from ASP.NET Web API

Loving ASP.NET Web API but not loving the .NET-centric PascalCase JSON it produces?

// a .NET class like this...public class Book{    public int NumberOfPages { get; set; }    public string Author { get; set; }}
// ... should be serialized into JSON like this{    "numberOfPages": 42,    "author": "JK Rowling"}

Luckily this is quite easy to fix with a custom formatter. I started with Henrik Nielsen’s custom Json.NET formatter and changed it slightly to use a camelCase resolver and also indent the JSON output (just for developers; we would turn this off once the app was deployed). You can grab the code here:

Then just swap out the default JSON formatter in your global.asax.cs:

var config = GlobalConfiguration.Configuration;// Replace the default JsonFormatter with our custom onevar index = config.Formatters.IndexOf(config.Formatters.JsonFormatter);config.Formatters[index] = new JsonCamelCaseFormatter();