How can i configure JSON format indents in ASP.NET Core Web API How can i configure JSON format indents in ASP.NET Core Web API asp.net asp.net

How can i configure JSON format indents in ASP.NET Core Web API


.NET Core 2.2 and lower:

In your Startup.cs file, call the AddJsonOptions extension:

services.AddMvc()    .AddJsonOptions(options =>    {        options.SerializerSettings.Formatting = Formatting.Indented;    });

Note that this solution requires Newtonsoft.Json.

.NET Core 3.0 and higher:

In your Startup.cs file, call the AddJsonOptions extension:

services.AddMvc()    .AddJsonOptions(options =>    {        options.JsonSerializerOptions.WriteIndented = true;    });

As for switching the option based on environment, this answer should help.


If you want to turn on this option for a single controller instead of for all JSON, you can have your controller return a JsonResult and pass the Formatting.Indented when constructing the JsonResult like this:

return new JsonResult(myResponseObject) { SerializerSettings = new JsonSerializerSettings() { Formatting = Formatting.Indented } };


In .NetCore 3+ you can achieve this as follows:

services.AddMvc()    .AddJsonOptions(options =>    {                        options.JsonSerializerOptions.WriteIndented = true;        });