Hello Devz,

JSON files are really common, we can see them everywhere. They provide a good structure to organize data, are simpler and lighter than XML files, and still human readable. In this simple example, we will show you how to use the Json.NET library (Newtonsoft) from NuGet. It is pretty simple to serialize and deserialize objects in C#. This tutorial will generate a list of employees, then serialize it to a JSON file to save it as a text file, and then deserialize it to display its content on screen in a simple console application.

Here is our Employee class:

using System; namespace JsonDemo { public class Employee { public Guid Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Title { get; set; } public decimal Salary { get; set; } } }
Code language: C# (cs)
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; namespace JsonDemo { class Program { static void Main(string[] args) { var jsonFileName = "employeeList.json"; List<Employee> employeeList = GenerateEmployeeList(); string jsonString = SaveToJsonFile(employeeList, jsonFileName); //Get the content from a JSON string List<Employee> loadedEmployeesListFromJsonString = LoadFromJsonString(jsonString); //Get the content from a JSON file List<Employee> loadedEmployeesListFromFile = LoadFromJsonFile(jsonFileName); foreach (var employee in loadedEmployeesListFromFile) { Console.WriteLine($"FirstName: {employee.FirstName} - LastName: {employee.LastName}"); } Console.WriteLine("Press any key..."); Console.ReadKey(); } private static List<Employee> GenerateEmployeeList() { var employeeList = new List<Employee>(); var employee1 = new Employee { Id = Guid.NewGuid(), FirstName = "Elon", LastName = "Musk", Title = "CEO", Salary = 37000 }; employeeList.Add(employee1); var employee2 = new Employee { Id = Guid.NewGuid(), FirstName = "Jeff", LastName = "Bezos", Title = "CEO", Salary = 28000 }; employeeList.Add(employee2); return employeeList; } private static string SaveToJsonFile(List<Employee> employeeList, string jsonFileName) { string jsonString = JsonConvert.SerializeObject(employeeList, Formatting.Indented); File.WriteAllText(jsonFileName, jsonString); return jsonString; } private static List<Employee> LoadFromJsonFile(string jsonFileName) { var employeeList = new List<Employee>(); using (StreamReader file = File.OpenText(jsonFileName)) { var serializer = new JsonSerializer(); employeeList = (List<Employee>)serializer.Deserialize(file, typeof(List<Employee>)); } return employeeList; } private static List<Employee> LoadFromJsonString(string jsonString) { List<Employee> result = JsonConvert.DeserializeObject<List<Employee>>(jsonString); return result; } } }
Code language: C# (cs)

Here is the content of our “employeeList.json” file:

[ { "Id": "3db405f0-200b-44f0-b464-fe10974f118d", "FirstName": "Elon", "LastName": "Musk", "Title": "CEO", "Salary": 37000.0 }, { "Id": "a1a73df0-d5a0-438c-ac75-3b5b15f89697", "FirstName": "Jeff", "LastName": "Bezos", "Title": "CEO", "Salary": 28000.0 } ]
Code language: C# (cs)

Please observe the options you can provide while serializing you object to JSON. In this case, we used “Formatting.Indented”, which allows us to display the content of the JSON file in a more human readable format with indentations.

Happy coding! 🙂