How to Read JSON file in .NET Core?

How to read a json file in .net core using c#.net. Here reading a json file data in string and convert it to class list object and display it.
In today's article I will show you how you can read a JSON file in .net core using c#.net in Visual Studio 2026. In this article I will use .net core 10. Here we will use console application using c#.net. Here we will use JsonConvert.DeserializeObject to read the JSON. So, for this article first we need to create a console application and install the NuGet package names as Newtonsoft.Json. 

Newtonsoft.Json

After installing the package. We need to add class file and the below class property. We will store the Json value in list. 

public class EmployeeDetail
{
    public string Name { get; set; }
    public string jobTitle { get; set; }
    public string employeeCode { get; set; }
}
 
In above code i have defined a class for employee and added some properties which is same property as our Json file.
 {
   "employees":[
      {
         "Name":"Frontend",
         "jobTitle":"Frontend Developer",
         "employeeCode":"E1",
      },
      {
         "Name":"Backend",
         "jobTitle":"Backend Developer",
         "employeeCode":"E2",
      },
      {
         "Name":"Fullstack",
         "jobTitle":"Developer",
         "employeeCode":"E3",
      }
   ]
} 
Now lets check the code to read this json file data into a List of class.
 using Newtonsoft.Json.Linq;

//Read JSON data from a file or other source
string json = string.Empty;
string jsonFilePath = "C:\\Personal\\Files\\employee.json";
if (File.Exists(jsonFilePath))
{
    json = File.ReadAllText(jsonFilePath);
    //Code to deserialize JSON data into a list of EmployeeDetail objects
    if (!string.IsNullOrEmpty(json))
    {
        var jsonObject = JObject.Parse(json);
        var token = jsonObject["employees"];
        List<EmployeeDetail> data;
        if (token?.Type == JTokenType.Array)
        {
            data = token?.ToObject<List<EmployeeDetail>>();
        }
        else
        {
            data = new List<EmployeeDetail> { token?.ToObject<EmployeeDetail>() };
        }
        foreach (var item in data)
        {
            Console.WriteLine($"Name: {item.Name}, Job Title: {item.JobTitle}, Employee Code: {item.EmployeeCode}");
        }
    }
    else
    {
        Console.WriteLine("json data is empty.");
    }
}
else
{
    Console.WriteLine("json file does not found.");
} 
In above code first I have first taken two variables one for path and second for Json string. Here we have validated file path, if it valid I have read the data from Json file and if file is not having null value, on that case I have parsed the parsed the Json string by using  JObject.Parse(json). 

After parsing the Json string i have read the token which is nothing but the table name. After that verified the type of Json token type by using JTokenType. If Token is array on that case i have used token?.ToObject to case the Json object to class list object. Now we have done run the code to check the output.

Json to Class List

In above we are able to read and convert the json string to list object. Now press F5 to check the output.

Json to Class List Output

Post a Comment