Using System.Text.Json i can pretty print json using serialization option.
var options = new JsonSerializerOptions{ WriteIndented = true };
jsonString = JsonSerializer.Serialize(typeToSerialize, options);
However, I have string JSON and don't know the concrete type. Ho do I pretty-print JSON string?
My old code was using Newtonsoft and I could do it without serialize/deserialize
public static string JsonPrettify(this string json)
{
if (string.IsNullOrEmpty(json))
{
return json;
}
using (var stringReader = new StringReader(json))
using (var stringWriter = new StringWriter())
{
var jsonReader = new JsonTextReader(stringReader);
var jsonWriter = new JsonTextWriter(stringWriter) { Formatting = Formatting.Indented };
jsonWriter.WriteToken(jsonReader);
return stringWriter.ToString();
}
}