39

How can I configure ASP.NET Core Web API controller to return pretty formatted JSON content for the Development environment only?

By default, it returns something like:

{"id":1,"code":"4315"} 

I would like to have indents in the response for readability:

{ "id": 1, "code": "4315" } 
1
  • There is usually a "prettify" button in browser which will make JSON readable. Fiddler also have a special tab fro JSON. Commented Aug 23, 2016 at 11:21

6 Answers 6

84

.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.

Sign up to request clarification or add additional context in comments.

3 Comments

Note that SerializerSettings is in Microsoft.AspNetCore.Mvc, but in order to use Formatting you need a using Newtonsoft.Json.
This is what I needed options.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
In 2.x we used to add a MediaTypeMapping for Json to be "application/json" (otherwise the browser would ignore the formatting). How do we add a similar mapping in 3.0 and higher?
18

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 } }; 

Comments

11

In .NET Core 3 (and later), you can achieve this as follows:

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

1 Comment

This is the best answer now. I just submitted a suggested edit to incorporate this into DavidG's accepted answer.
1

In my project, I used Microsoft.AspNetCore.Mvc with the code below for all controllers. This for .NET Core 3.

public void ConfigureServices(IServiceCollection services) { services.AddControllers() .AddNewtonsoftJson(options => { options.SerializerSettings.Formatting = Formatting.Indented; }); } 

Comments

1

In .NET 7, if you want to return indented JSON output and you are using controllers, you can use this in your startup code:

builder.Services.AddControllers() .AddJsonOptions(options => { options.JsonSerializerOptions.WriteIndented = true; }); 

This is using the built-in JSON capabilities in .NET, not using any external frameworks like Newtonsoft.

Comments

0

If you want this option only for specific action, using System.Text.Json

return new JsonResult(myResponseObject) { SerializerSettings = new JsonSerializerOptions() { WriteIndented = true } }; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.