Last active
March 15, 2024 05:05
-
-
Save wipiano/dea1e30f71d8e1269bedc0ad259f040a to your computer and use it in GitHub Desktop.
format arbitrary JSON string with System.Text.Json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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