Formatting JSON as plain text with proper indentation can make it much easier to include in emails or other readable contexts. Here's a quick tip/guide on how to achieve that in .NET.
To pretty-print a JSON string in .NET and make it suitable for inclusion in an email, text page, a string, or any other thing, you can format the JSON with proper indentation and ensure it is styled as plain text for easy readability.
Code Example
Let's have a look at how we can implement this method on strings in our code.
using System;
using System.Text.Json;
class Program
{
static void Main()
{
string jsonString = "{\"name\":\"Christian\",\"age\":28,\"country\":\"Denmark\"}";
// Pretty-print the JSON string
string prettyJson = PrettyPrintJson(jsonString);
// Print the result
Console.WriteLine(prettyJson);
}
static string PrettyPrintJson(string jsonString)
{
var jsonElement = JsonDocument.Parse(jsonString).RootElement;
return JsonSerializer.Serialize(jsonElement, new JsonSerializerOptions { WriteIndented = true });
}
}
What happens above? 🤔 Let me explain.
Parsing the JSON text - The JsonDocument.Parse method reads and parses the JSON string.
Serialization with Indentation - The JsonSerializer.Serialize method outputs the JSON string with indentation by setting WriteIndented to true in the options.
As An Extension Method
using System.Text.Json;
namespace <YourNameSpace-UPDATE-ME>;
public static class StringExtensions
{
/// <summary>
/// Pretty prints a JSON string.
/// </summary>
/// <param name="jsonString"></param>
/// <returns></returns>
public static string PrettyPrintJson(this string jsonString)
{
JsonElement jsonElement = JsonDocument.Parse(jsonString).RootElement;
return JsonSerializer.Serialize(jsonElement, new JsonSerializerOptions { WriteIndented = true });
}
}
With this, you can easily extend logic on your default strings to return a string in JSON format in a pretty way. 👌
I hope you solved a problem in your code with this short tutorial. If you have any questions, please let me know in the comments below. ✌️
My name is Christian. I am a 28-year-old Solution Architect & Software Engineer with a passion for .NET, Cloud, and Containers. I love to share my knowledge and teach other like-minded about tech.
Use JsonSerializer to Pretty-Print JSON in .NET
Formatting JSON as plain text with proper indentation can make it much easier to include in emails or other readable contexts. Here's a quick tip/guide on how to achieve that in .NET.
— Christian Schou Køster
Use JsonSerializer to Pretty-Print JSON in .NET
To pretty-print a JSON string in .NET and make it suitable for inclusion in an email, text page, a string, or any other thing, you can format the JSON with proper indentation and ensure it is styled as plain text for easy readability.
Code Example
Let's have a look at how we can implement this method on strings in our code.
What happens above? 🤔 Let me explain.
JsonDocument.Parse
method reads and parses the JSON string.JsonSerializer.Serialize
method outputs the JSON string with indentation by settingWriteIndented
totrue
in the options.As An Extension Method
With this, you can easily extend logic on your default strings to return a string in JSON format in a pretty way. 👌
I hope you solved a problem in your code with this short tutorial. If you have any questions, please let me know in the comments below. ✌️
Resources
My name is Christian. I am a 28-year-old Solution Architect & Software Engineer with a passion for .NET, Cloud, and Containers. I love to share my knowledge and teach other like-minded about tech.