Write to JSON file in C#
This post will discuss how to write to JavaScript Object Notation (JSON) file in C#.
1. Using JsonSerializer.Serialize() method
The recommended solution to serialize and deserialize JSON in .NET 4.7.2 and above, is using the System.Text.Json namespace. You can use the JsonSerializer class with custom types to serialize from and deserialize into. For serialization and deserialization, you also need to include the System.Text.Json.Serialization namespace.
The following solution shows the use of the JsonSerializer.Serialize() method for converting an object to a JSON string. It then writes the JSON string to a file using the File.WriteAllText() method.
|
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.IO; 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 Program { public static void Main() { List<Customer> customers = new List<Customer>(); customers.Add(new Customer {Name = "Jason", Age = 25}); customers.Add(new Customer {Name = "Nikki", Age = 20}); string json = JsonSerializer.Serialize(customers); File.WriteAllText(@"C:\customers.json", json); } } |
Customers.json:
[{"Name":"Jason","Age":25},{"Name":"Nikki","Age":20}]
To pretty-print the JSON string, you can set the JsonSerializerOptions.WriteIndented to true. However, formatting the JSON might have a negative impact on performance.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System.IO; 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 Program { public static void Main() { List<Customer> customers = new List<Customer>(); customers.Add(new Customer {Name = "Jason", Age = 25}); customers.Add(new Customer {Name = "Nikki", Age = 20}); var options = new JsonSerializerOptions { WriteIndented = true }; string json = JsonSerializer.Serialize(customers, options); File.WriteAllText(@"C:\customers.json", json); } } |
Customers.json:
[
{
"Name": "Jason",
"Age": 25
},
{
"Name": "Nikki",
"Age": 20
}
]
2. Using JsonConvert.SerializeObject() method
Alternatively, you can use the Json.NET library to serialize to and deserialize from JSON. The Newtonsoft.Json namespace provides the JsonConvert class that provides several methods for converting between .NET types and JSON types.
The following example serializes an object to JSON using the SerializeObject() method and then outputs the JSON string to a file using the File.WriteAllText() 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.IO; using System.Collections.Generic; using Newtonsoft.Json; public class Customer { public string Name { get; set; } public int Age { get; set; } } public class Program { public static void Main() { List<Customer> customers = new List<Customer>(); customers.Add(new Customer {Name = "Jason", Age = 25}); customers.Add(new Customer {Name = "Nikki", Age = 20}); string json = JsonConvert.SerializeObject(customers.ToArray()); File.WriteAllText(@"C:\customers.json", json); } } |
Customers.json:
[{"Name":"Jason","Age":25},{"Name":"Nikki","Age":20}]
To format the JSON for readability purpose, you can pass the Formatting.Indented option to the JsonConvert.SerializeObject() 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.IO; using System.Collections.Generic; using Newtonsoft.Json; public class Customer { public string Name { get; set; } public int Age { get; set; } } public class Program { public static void Main() { List<Customer> customers = new List<Customer>(); customers.Add(new Customer {Name = "Jason", Age = 25}); customers.Add(new Customer {Name = "Nikki", Age = 20}); string json = JsonConvert.SerializeObject(customers.ToArray(), Formatting.Indented); File.WriteAllText(@"C:\customers.json", json); } } |
Customers.json:
[
{
"Name": "Jason",
"Age": 25
},
{
"Name": "Nikki",
"Age": 20
}
]
3. Using JavaScriptSerializer.Serialize() method
Finally, for AJAX applications, you can use the JavaScriptSerializer class for serialization and deserialization. To serialize an object to a JSON string, you can use its Serialize() method. 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 25 |
using System.IO; using System.Collections.Generic; using System.Web.UI; using System.Web.Script.Serialization; public class Customer { public string Name { get; set; } public int Age { get; set; } } public class Program { public static void Main() { List<Customer> customers = new List<Customer>(); customers.Add(new Customer {Name = "Jason", Age = 25}); customers.Add(new Customer {Name = "Nikki", Age = 20}); var serializer = new JavaScriptSerializer(); var json = serializer.Serialize(customers); File.WriteAllText(@"C:\customers.json", json); } } |
Customers.json:
[{"Name":"Jason","Age":25},{"Name":"Nikki","Age":20}]
That’s all about writing to JavaScript Object Notation (JSON) file 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 :)