Skip to content

Instantly share code, notes, and snippets.

@wipiano
Last active March 15, 2024 05:05
Show Gist options
  • Save wipiano/dea1e30f71d8e1269bedc0ad259f040a to your computer and use it in GitHub Desktop.
Save wipiano/dea1e30f71d8e1269bedc0ad259f040a to your computer and use it in GitHub Desktop.
format arbitrary JSON string with System.Text.Json
string json = """
{ "X": 123, "Y": { "Y1": 1, "Y2": 2 } }
""";
JsonSerializerOptions options = new(JsonSerializerDefaults.General) { WriteIndented = true };
string formatted = JsonSerializer.Serialize(JsonSerializer.Deserialize<JsonElement>(json), options);
Console.WriteLine(formatted);
/* output:
{
"X": 123,
"Y": {
"Y1": 1,
"Y2": 2
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment