Indented Pretty Print Formatting for JSON in ASP.NET Core

tldr;

See line 13


public class Startup
{
private readonly IWebHostEnvironment _environment;
public Startup(IWebHostEnvironment environment)
{
_environment = environment;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddJsonOptions(options => options.JsonSerializerOptions.WriteIndented = _environment.IsDevelopment());
}
// Other code ommitted for brevity
}

view raw

Startup.cs

hosted with ❤ by GitHub

 

The Problem – Readability

By default, JSON in ASP.NET Core will not be indented at all.  By that I mean, the JSON is returned without any spacing or formatting, and in the cheapest way possible (from a “bytes-sent-over-the-wire” perspective).

Example:

2019-10-29_21-32-53

 

This is usually what you want when in Production in order to decrease network bandwidth traffic.  However, as the developer, if you’re trying to troubleshoot or parse this data with your eyes, it can be a little difficult to make out what’s what.  Particularly if you have a large dataset and if you’re looking for a needle in a hay stack.

 

The Solution – Indented Formatting

ASP.NET Core ships with the ability to make the JSON format as indented (aka “pretty print”) to make it more readable.  Further, we can leverage the Environment in ASP.NET Core to only do this when in the Development environment for local troubleshooting and keep the bandwidth down in Production, as well as have the server spend less CPU cycles doing the formatting.

In ASP.NET Core 3.0 using the new System.Text.Json formatter:


public class Startup
{
private readonly IWebHostEnvironment _environment;
public Startup(IWebHostEnvironment environment)
{
_environment = environment;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddJsonOptions(options => options.JsonSerializerOptions.WriteIndented = _environment.IsDevelopment());
}
// Other code ommitted for brevity
}

view raw

Startup.cs

hosted with ❤ by GitHub

 

In ASP.NET Core 2.2 and prior:


public class Startup
{
private readonly IHostingEnvironment _hostingEnvironment;
public Startup(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddJsonOptions(options =>
{
options.SerializerSettings.Formatting = _hostingEnvironment.IsDevelopment()
? Formatting.Indented
: Formatting.None;
});
}
// Other code ommitted for brevity
}

view raw

Startup.cs

hosted with ❤ by GitHub

 

Now when I run the app, the JSON looks much cleaner and easier to read.

2019-10-29_21-56-58

Chrome Extensions

There are also Chrome Extensions that will do this for you as well when you navigate to a URL that returns an application/json Content-Type.  Some extensions add more formatting capabilities (such as colorization and collapsing).  For instance, I use JSON Formatter as my go-to extension for this.

2019-10-29_22-01-50

 

Closing – So why do I need this if I have the Chrome Extension?

While I use the JSON Formatter extension, I still like setting the indented format in ASP.NET Core for a few reasons:

  1. Not all of my team members use the same Chrome extensions I do.
  2. The formatting in ASP.NET Core will make it show up the same everywhere – the browser (visiting the API URL directly), DevTools, Fiddler, etc.  JSON Formatter only works in Chrome when hitting the API URL directly (not viewing it in DevTools).
  3. Some applications I work on use JWT’s stored in local or session storage, so JSON Formatter doesn’t help me out here.  JSON Formatter only works when I can navigate to the API URL directly, which works fine when I’m using Cookie auth or an API with no auth, but that does not work when I’m using JWT’s which don’t get auto-shipped to the server like Cookies do.

 

Hope this helps!