NashTech Blog

Table of Contents

JSON (JavaScript Object Notation) is a popular lightweight data format used for data interchange. In .NET, handling JSON data is straightforward due to robust libraries like System.Text.Json and Newtonsoft.Json. This blog will guide you through reading from and writing to JSON files in .NET using these two libraries.

Why Use JSON?

JSON is widely used for configurations, data exchange between APIs, and storage due to its lightweight structure. Its simplicity makes it an excellent choice for scenarios where XML is too verbose or other formats are overkill.

Using System.Text.Json (Built-in .NET Library)

Introduced in .NET Core 3.0, the System.Text.Json library offers fast, flexible, and efficient ways to handle JSON.

Reading JSON

To read a JSON file, you typically deserialize the content into a C# object. Here’s how you can do it:

  1. Create a model class to match the structure of your JSON.
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}
  1. Use the JsonSerializer class to deserialize the JSON content:
using System.Text.Json;
string jsonFilePath = "person.json";
string jsonString = File.ReadAllText(jsonFilePath);
Person person = JsonSerializer.Deserialize<Person>(jsonString);
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");

Writing JSON

Writing a C# object to a JSON file is done using serialization. Here’s an example:

using System.Text.Json;
Person person = new Person
{
    Name = "John Doe",
    Age = 30
};
string jsonString = JsonSerializer.Serialize(person);
File.WriteAllText("output.json", jsonString);

This will create or overwrite output.json with the JSON representation of the Person object.

Using Newtonsoft.Json (Popular External Library)

Though System.Text.Json is built-in, Newtonsoft.Json (also known as Json.NET) is highly popular and widely used. If you’re working on older versions of .NET or need advanced features, Newtonsoft.Json is a great choice.

To install the package, use NuGet:

dotnet add package Newtonsoft.Json

Reading JSON

Reading JSON with Newtonsoft.Json is simple and similar to System.Text.Json:

  1. Create the same Person model class.
  2. Deserialize the JSON content:
using Newtonsoft.Json;
string jsonFilePath = "person.json";
string jsonString = File.ReadAllText(jsonFilePath);
Person person = JsonConvert.DeserializeObject<Person>(jsonString);
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");

Writing JSON

To write a C# object to a JSON file, use JsonConvert.SerializeObject:

using Newtonsoft.Json;
Person person = new Person
{
    Name = "Jane Doe",
    Age = 28
};
string jsonString = JsonConvert.SerializeObject(person, Formatting.Indented);
File.WriteAllText("output_newtonsoft.json", jsonString);

The Formatting.Indented option provides more readable output with proper indentation.

Conclusion

Both System.Text.Json and Newtonsoft.Json offer efficient methods to handle JSON data in .NET. For most modern applications, System.Text.Json is a great built-in choice, but if you need advanced features or backward compatibility, Newtonsoft.Json is a solid alternative.

By understanding both approaches, you can confidently work with JSON files in your .NET applications, leveraging the strengths of each library as needed.

Picture of Vipul Kumar

Vipul Kumar

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

Scroll to Top