Readable JSON makes logs, API responses and configuration files much easier to work with. In .NET, System.Text.Json can produce indented output directly. This guide covers the common cases: serializing objects as indented JSON, re-formatting an existing JSON string, customizing indentation, and minifying.

Serialize an object as indented JSON

Set WriteIndented on JsonSerializerOptions:

using System.Text.Json;

var order = new { Id = 1042, Customer = "Ada", Items = new[] { "keyboard", "mouse" } };

var options = new JsonSerializerOptions { WriteIndented = true };
string json = JsonSerializer.Serialize(order, options);

Output:

{
  "Id": 1042,
  "Customer": "Ada",
  "Items": [
    "keyboard",
    "mouse"
  ]
}

Create the options once and reuse them. JsonSerializerOptions caches type metadata, so creating a new instance for every call is noticeably slower.

private static readonly JsonSerializerOptions IndentedOptions = new() { WriteIndented = true };

Re-format an existing JSON string

When you already have JSON text, for example from an HTTP response, parse it and write it back out. You do not need a C# type for the data.

using System.Text.Json;
using System.Text.Json.Nodes;

static string PrettyPrint(string json)
{
    JsonNode? node = JsonNode.Parse(json);
    return node?.ToJsonString(IndentedOptions) ?? "null";
}

JsonNode.Parse throws a JsonException when the input is not valid JSON, which makes it a quick validity check as well. If the input may contain comments or trailing commas, allow them explicitly:

var documentOptions = new JsonDocumentOptions
{
    CommentHandling = JsonCommentHandling.Skip,
    AllowTrailingCommas = true,
};

JsonNode? node = JsonNode.Parse(json, documentOptions: documentOptions);

Customize indentation

Since .NET 9 you can choose the indent character, the indent size and the line ending:

var options = new JsonSerializerOptions
{
    WriteIndented = true,
    IndentCharacter = '\t',
    IndentSize = 1,
    NewLine = "\n",
};
Option Default Allowed values
IndentCharacter space space or tab
IndentSize 2 0 to 127
NewLine Environment.NewLine \n or \r\n

Keep non-ASCII characters readable

By default System.Text.Json escapes non-ASCII and HTML-sensitive characters, so "café" is written as "caf\u00E9". That is safe, but harder to read. For output that is shown to people and never embedded in HTML, use the relaxed encoder:

using System.Text.Encodings.Web;

var readable = new JsonSerializerOptions
{
    WriteIndented = true,
    Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};

Only use UnsafeRelaxedJsonEscaping when the JSON will not be placed inside an HTML page or script. The default encoder escapes characters such as < and > precisely to prevent script injection.

Minify JSON

Indentation is off by default, so serializing with default options produces compact output. To minify an existing string, parse it and write it without indentation:

static string Minify(string json) => JsonNode.Parse(json)?.ToJsonString() ?? "null";

Summary

  • Use WriteIndented = true to pretty-print, and reuse the JsonSerializerOptions instance.
  • Re-format existing JSON with JsonNode.Parse(json).ToJsonString(options).
  • Use IndentCharacter, IndentSize and NewLine (.NET 9 and later) to match your style.
  • Use the relaxed encoder only for human-readable output that never ends up in HTML.