Skip to main content
Back to Blog

How to Format JSON in C# (.NET)

Feb 07, 2026 3 min read

Since .NET Core 3.0, the System.Text.Json namespace provides ultra-fast built-in functionality to handle JSON data. Simply apply the WriteIndented flag in your serializer options to get pretty output.

Example Code

C#
using System;
using System.Text.Json;

public class Program
{
    public static void Main()
    {
        var user = new { Name = "John", Age = 30, City = "New York" };

        var options = new JsonSerializerOptions 
        { 
            WriteIndented = true,
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase
        };

        string formattedJson = JsonSerializer.Serialize(user, options);
        Console.WriteLine(formattedJson);
    }
}

Common Use Cases

  • Configuring ASP.NET Core endpoints to output pretty printed JSON in development mode
  • Writing explicitly typed structures into appsettings files
  • Generating JSON documents dynamically

💡 Pro Tips for C#

  • In modern C#, you do not strictly require Newtonsoft.Json anymore! System.Text.Json is built-in and secure.
  • Setting PropertyNamingPolicy = JsonNamingPolicy.CamelCase guarantees your output keys start with a lowercase letter which follows JSON payload industry standards.

Modern .NET Tools

Need to beautify <code>System.Text.Json</code> output? Our formatter supports all modern JSON standards with high-performance parsing.