In this article, I will discuss three popular methods for handling JSON in .NET. JSON (JavaScript Object Notation) is a lightweight and text-based format commonly used for data exchange. .NET offers multiple approaches to working with JSON, each suited to specific scenarios and offering unique benefits. You have several libraries to choose from, each tailored to different requirements. Here are three popular options: Newtonsoft.Json, System.Text.Json, and NetJSON, along with code examples to help you get started.
Working With Json in .Net
- Using Newtonsoft.Json (Json.NET)
- Using System.Text.Json (Built-in .NET Core Library)
- Using NetJSON (High-Performance JSON Library)
Let's create an employee class and prepare data to work with different JSON parsing libraries.
public class Employee
{
public int Id { get; set; }
public string? Name { get; set; }
public string? Department { get; set; }
}
Now, prepare the data with a list of employees.
static List<Employee> Getemployees()
{
var employees = new List<Employee>
{
new Employee { Id = 1, Name = "John Doe", Department = "HR" },
new Employee { Id = 2, Name = "Jane Smith", Department = "IT" },
new Employee { Id = 3, Name = "Mike Johnson", Department = "Finance" }
};
return employees;
}
Using Newtonsoft.Json (Json.NET)
How do you install the Newtonsoft.Json package using the Manage NuGet Packages feature?
Install Newtonsoft.Json package
Search for the Newtonsoft.Json package in the NuGet package list as shown below.
Search package and install package
Let's look at an example where we serialize a list of employees into a JSON string and then deserialize the JSON string back into a list of employees.
static void JsonUsingNewtonsoft(List<Employee> employees)
{
// Serialize to JSON
string json = JsonConvert.SerializeObject(employees);
Console.WriteLine("Serialized JSON (Newtonsoft.Json):");
Console.WriteLine(json);
// Deserialize back to Employee object
List<Employee> employeeList = JsonConvert.DeserializeObject<List<Employee>>(json);
Console.WriteLine("\nDeserialized Employee:");
foreach (Employee employee in employeeList)
{
Console.WriteLine($"Id: {employee.Id}, Name: {employee.Name}, Department: {employee.Department}");
}
Console.ReadLine();
}
The snippet above shows the output of the code using the Newtonsoft.Json package.
Using System.Text.Json (Built-in .NET Core Library)
System.Text.Json, introduced in .NET Core 3.0, is the default and highly efficient library for processing JSON in .NET. It offers powerful features for serialization, deserialization, and parsing JSON documents.
static void JsonUsingSystemTextJson(List<Employee> employees)
{
// Serialize to JSON using System.Text.Json
string json = JsonSerializer.Serialize(employees);
Console.WriteLine("Serialized JSON (System.Text.Json):");
Console.WriteLine(Environment.NewLine);
Console.WriteLine(json);
// Deserialize back to Employee object using System.Text.Json
List<Employee> employeeList = JsonSerializer.Deserialize<List<Employee>>(json);
Console.WriteLine("\nDeserialized Employee (System.Text.Json):");
foreach (Employee employee in employeeList)
{
Console.WriteLine($"Id: {employee.Id}, Name: {employee.Name}, Department: {employee.Department}");
}
}
The snippet above shows the output of the code using the System.Text.Json package.
Using NetJSON (High-Performance JSON Library)
NetJSON is a high-performance, lightweight JSON library for .NET, designed for speed and minimal memory usage. It is ideal for performance-critical scenarios, such as processing large datasets, high-throughput applications, or real-time systems. While it lacks the extensive customization options of libraries like Newtonsoft.Json, it stands out for its simplicity and exceptional performance.
Install NetJSON Package from NuGet Package Manager
Here’s an example demonstrating the use of the NetJSON package to serialize a list of employees into a JSON string and then deserialize the JSON string back into a list of employees.
static void JsonUsingNetJSON(List<Employee> employees)
{
// Serialize to JSON using NetJSON
string json = NetJSON.NetJSON.Serialize(employees);
Console.WriteLine("Serialized JSON (NetJSON):");
Console.WriteLine(Environment.NewLine);
Console.WriteLine(json);
// Deserialize back to Employee object using NetJSON
List<Employee> employeeList = NetJSON.NetJSON.Deserialize<List<Employee>>(json);
Console.WriteLine("\nDeserialized Employee (NetJSON):");
foreach (Employee employee in employeeList)
{
Console.WriteLine($"Id: {employee.Id}, Name: {employee.Name}, Department: {employee.Department}");
}
}
The snippet above shows the output of the code using the NetJSON package.
Let's put all methods together in a single class.
using JsonExample;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;
var employeeList = Getemployees();
// Using Newtonsoft.Json
JsonUsingNewtonsoft(employeeList);
// Using System.Text.Json .Net Core built-in library
JsonUsingSystemTextJson(employeeList);
// Using NetJSON
JsonUsingNetJSON(employeeList);
Console.ReadLine();
static List<Employee> Getemployees()
{
var employees = new List<Employee>
{
new Employee { Id = 1, Name = "John Doe", Department = "HR" },
new Employee { Id = 2, Name = "Jane Smith", Department = "IT" },
new Employee { Id = 3, Name = "Mike Johnson", Department = "Finance" }
};
return employees;
}
static void JsonUsingNewtonsoft(List<Employee> employees)
{
// Serialize to JSON
string json = JsonConvert.SerializeObject(employees);
Console.WriteLine("Serialized JSON (Newtonsoft.Json):");
Console.WriteLine(Environment.NewLine);
Console.WriteLine(json);
// Deserialize back to Employee object
List<Employee> employeeList = JsonConvert.DeserializeObject<List<Employee>>(json);
Console.WriteLine("\nDeserialized Employee:");
foreach (Employee employee in employeeList)
{
Console.WriteLine($"Id: {employee.Id}, Name: {employee.Name}, Department: {employee.Department}");
}
}
static void JsonUsingSystemTextJson(List<Employee> employees)
{
// Serialize to JSON using System.Text.Json
string json = JsonSerializer.Serialize(employees);
Console.WriteLine("Serialized JSON (System.Text.Json):");
Console.WriteLine(Environment.NewLine);
Console.WriteLine(json);
// Deserialize back to Employee object using System.Text.Json
List<Employee> employeeList = JsonSerializer.Deserialize<List<Employee>>(json);
Console.WriteLine("\nDeserialized Employee (System.Text.Json):");
foreach (Employee employee in employeeList)
{
Console.WriteLine($"Id: {employee.Id}, Name: {employee.Name}, Department: {employee.Department}");
}
}
static void JsonUsingNetJSON(List<Employee> employees)
{
// Serialize to JSON using NetJSON
string json = NetJSON.NetJSON.Serialize(employees);
Console.WriteLine("Serialized JSON (NetJSON):");
Console.WriteLine(Environment.NewLine);
Console.WriteLine(json);
// Deserialize back to Employee object using NetJSON
List<Employee> employeeList = NetJSON.NetJSON.Deserialize<List<Employee>>(json);
Console.WriteLine("\nDeserialized Employee (NetJSON):");
foreach (Employee employee in employeeList)
{
Console.WriteLine($"Id: {employee.Id}, Name: {employee.Name}, Department: {employee.Department}");
}
}
Recommendation. Choosing the Right JSON Library in the Real-time Project.
- System.Text.Json: Best suited for modern .NET projects due to its performance and native integration.
- Newtonsoft.Json: Ideal for projects requiring compatibility or advanced customization features.
- NetJSON: Recommended for performance-critical scenarios such as real-time systems or large-scale data processing.
Note. Each approach serves different needs, and your choice depends on project requirements and performance considerations
Summary
This article covers three popular methods for working with JSON in .NET, highlighting their unique advantages and ideal use cases. JSON is a lightweight, text-based format commonly used for data exchange, and .NET provides versatile tools to handle it effectively.
Note. I have provided a working solution compatible with Visual Studio 2022, which you can download from the top left corner of the article. Simply click the download icon to get the .zip file.
If you'd like to explore more articles on .Net Core, please visit the links below.
- Understanding Model Binding in ASP.NET Core with .NET 8
- CRUD Operation using Elastic Search And .Net Core API
- Export Data In EXCEL, PDF, CSV, Word, JSON, XML And Text File In .NET Core 3.1 Using MVC Core
- CRUD Operation With .NET Core 3.1 And Entity Framework Core
- Difference Between Var, Dynamic And Object type In C#