Get formatted JSON in C#
This post will discuss how to get formatted JSON in C#.
By default, JSON is serialized without any white space. To improve readability, it is often desired to get an indented JSON. There are several options to achieve that in C#:
1. Using JsonSerializer.Serialize() method
For .NET versions 4.7.2 and above, you can use the JsonSerializer.Serialize() method from System.Text.Json namespace to format a JSON string. It accepts the JsonSerializerOptions class to control the conversion behavior. The WriteIndented property indicates whether JSON should use pretty printing. To pretty-print the JSON, set the WriteIndented option to true, as the following example illustrates.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; using System.Collections.Generic; using System.Text.Json; using System.Text.Json.Serialization; public class Customer { public string Name { get; set; } public int Age { get; set; } } public class Example { public static void Main() { List<Customer> customers = new List<Customer>(); customers.Add(new Customer {Name = "Chris", Age = 25}); customers.Add(new Customer {Name = "Jennifer", Age = 20}); var options = new JsonSerializerOptions { WriteIndented = true }; string json = JsonSerializer.Serialize(customers, options); Console.WriteLine(json); } } |
Output:
[
{
"Name": "Chris",
"Age": 25
},
{
"Name": "Jennifer",
"Age": 20
}
]
To prettify a JSON string, you can deserialize it first using the JsonSerializer.Deserialize() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; using System.Text.Json; using System.Text.Json.Serialization; public class Customer { public string? Name { get; set; } public int Age { get; set; } } public class Example { public static void Main() { string jsonString = "[{\"Name\":\"Chris\",\"Age\":25},{\"Name\":\"Jennifer\",\"Age\":20}]"; var parsedJson = JsonSerializer.Deserialize<List<Customer>>(jsonString); var options = new JsonSerializerOptions { WriteIndented = true }; string formattedJson = JsonSerializer.Serialize(parsedJson, options); Console.WriteLine(formattedJson); } } |
Output:
[
{
"Name": "Chris",
"Age": 25
},
{
"Name": "Jennifer",
"Age": 20
}
]
2. Using JsonConvert.SerializeObject() method
Another option is to use the JsonConvert.SerializeObject() method (available in Newtonsoft.Json namespace) from the Json.NET library to serialize an object to a formatted JSON string. The following code example demonstrates calling this method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.Collections.Generic; using Newtonsoft.Json; public class Customer { public string Name { get; set; } public int Age { get; set; } } public class Example { public static void Main() { List<Customer> customers = new List<Customer>(); customers.Add(new Customer {Name = "Chris", Age = 25}); customers.Add(new Customer {Name = "Jennifer", Age = 20}); string json = JsonConvert.SerializeObject(customers.ToArray(), Formatting.Indented); Console.WriteLine(json); } } |
Output:
[
{
"Name": "Chris",
"Age": 25
},
{
"Name": "Jennifer",
"Age": 20
}
]
The following code converts an existing JSON string to a formatted JSON string, without deserializing it into a typed object.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using Newtonsoft.Json; public class Example { public static void Main() { string jsonString = "[{\"Name\":\"Chris\",\"Age\":25},{\"Name\":\"Jennifer\",\"Age\":20}]"; var parsedJson = JsonConvert.DeserializeObject(jsonString); string formattedJson = JsonConvert.SerializeObject(parsedJson, Formatting.Indented); Console.WriteLine(formattedJson); } } |
Output:
[
{
"Name": "Chris",
"Age": 25
},
{
"Name": "Jennifer",
"Age": 20
}
]
3. Using JToken.FromObject() method
Alternatively, you can use the JToken.FromObject() method (available in Newtonsoft.Json.Linq namespace) from the Json.NET library that returns a JToken with the value of the specified object. Then invoke the ToString() on this token, which returns the indented JSON.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using Newtonsoft.Json.Linq; using System.Collections.Generic; public class Customer { public string Name { get; set; } public int Age { get; set; } } public class Example { public static void Main() { List<Customer> customers = new List<Customer>(); customers.Add(new Customer {Name = "Chris", Age = 25}); customers.Add(new Customer {Name = "Jennifer", Age = 20}); string formattedJson = JToken.FromObject(customers).ToString(); Console.WriteLine(formattedJson); } } |
Output:
[
{
"Name": "Chris",
"Age": 25
},
{
"Name": "Jennifer",
"Age": 20
}
]
To prettify an existing JSON without deserializing it into a .NET type, you can do like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using Newtonsoft.Json.Linq; public class Example { public static void Main() { string jsonString = "[{\"Name\":\"Chris\",\"Age\":25},{\"Name\":\"Jennifer\",\"Age\":20}]"; string formattedJson = JToken.Parse(jsonString).ToString(); Console.WriteLine(formattedJson); } } |
Output:
[
{
"Name": "Chris",
"Age": 25
},
{
"Name": "Jennifer",
"Age": 20
}
]
That’s all about getting formatted JSON in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)