Customize Json result in Web API

By Ugo Lattanzi on July 18th , 2012 in Various WebAPI WebDev | comments

Share On Google Share On Facebook Share On Twitter

In my previous post I wrote about Web API and the content negotiation. In this post I’d like to describe how it’s easy to change the Json serialization.

The most important improvement from the Beta to the RC in the Web API is about the serialization, in fact now the framework is using Json.Net for the serialization. For all people who don’t know what Json.Net is I can say, to make the definition easily, Json.Net is the best serializer in the .NET world.

It's cool for many reasons, especially because it's flexible and easy to setup. In this post I’d like to show the principals and useful settings that you probably want to change in your application, or can be helpful for you in the future.

Before to start, we have to make our Web API project easy to debug so, I’m going to remove the XML formatter. I’m doing that because I’m in a test project and I’d like to see the response in the browser. The easily way to force the response to Json for all Web API requests is to remove the XML. Obviously you shouldn't do it in production.

NOTE: All code in this post,except the last one, must be located into the global.asax.cs

var formatters = GlobalConfiguration.Configuration.Formatters;


Now we can start change the setting for all Json responses accessing to GlobalConfiguration.Configuration.Formatters.JsonFormatter.

In the following examples I’ll use always the class below:

public class User
  public string Firstname { get; set; }
  public string Lastname { get; set; }
  public string Username { get; set; }
  public DateTime Birthdate { get; set; }
  public Uri Website { get; set; }
  public int Age { get; set; }
  public double Salary { get; set; }

  public string IgnoreProperty { get; set; }

How can we indent the json response?

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;

json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;

How can we change the case in the response?

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;

json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

How can we manage the null in the response?

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;

json.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;

How can we change the DateTime format?

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;

json.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;

How can we change the TimeZone format?

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;

json.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;

How can we change the Culture of the serializer?

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;

json.SerializerSettings.Culture = new CultureInfo("it-IT");

Another cool feature of Web API is to opportunity to override the configuration for a single response. You can use all of the previous setting directly in the single action like explained in the code below:

public HttpResponseMessage Get(){
  IList<User> result = new List<User>();
  result.Add(new User
                 Age = 34,
                 Birthdate = DateTime.Now,
                 Firstname = "Ugo",
                 Lastname = "Lattanzi",
                 IgnoreProperty = "This text should not appear in the reponse",
                 Salary = 1000,
                 Username = "imperugo",
                 Website = new Uri("")

  var formatter = new JsonMediaTypeFormatter();
  var json =formatter.SerializerSettings;

  json.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
  json.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
  json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
  json.Formatting = Newtonsoft.Json.Formatting.Indented;
  json.ContractResolver = new CamelCasePropertyNamesContractResolver();
  json.Culture = new CultureInfo("it-IT");

  return Request.CreateResponse(HttpStatusCode.OK, result, formatter);

Such as ASP.NET MVC, Web API are really flexible ... and ROCKS!