In this post, we are going to see how to work easily and efficiently with Json files in a C# program thanks to the Newtownsoft Json.NET library.
There is no doubt that the Json format has become one of the standards for exchanging structured data between cross-platform Web services.
The Json format has some advantages over its competitors (like xml), such as a smaller file size, being easier for a person to understand, and faster generation and processing.
Although we usually associate Json files with Javascript, the main programming languages have libraries to work with Json files comfortably.
Of course, C# is no exception. The .NET framework has natively supported Json since version 4.0. But its use is even simpler and more efficient thanks to the popular Open Source library Json.NET, whose code is available at https://github.com/JamesNK/Newtonsoft.Json.
Using the Json.NET library
Adding the Json.NET library to our program is easy as it is available as a NuGet package. We simply have to add it through the package manager and we’ll be ready to work.

Let’s briefly see the use of Json.NET. For this, we create an object that has the structure of the Json we want to work with.
public class Product
{
public string Name { get; set; }
public DateTime ExpiryDate { get; set; }
public decimal Price { get; set; }
public string[] Sizes { get; set; }
}
Serialize Json
To perform serialization, we simply have to call the SerializeObject function.
var product = new Product
{
Name = "Apple",
ExpiryDate = new DateTime(2008, 12, 28),
Price = 3.99M,
Sizes = new[] {"Small", "Medium", "Large"}
};
string json = JsonConvert.SerializeObject(product);
Now with this string we can do whatever we need, such as saving it to a file.
string path = @"c:\product.json";
System.IO.File.WriteAllText(path, json);
Or send it via an HTTP request.
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/xml; charset=utf-8";
request.Timeout = 30000;
string json = JsonConvert.SerializeObject(product);
byte[] byteArray = Encoding.UTF8.GetBytes(json);
request.ContentLength = byteArray.Length;
var dataStream = new StreamWriter(request.GetRequestStream());
dataStream.Write(byteArray);
dataStream.Close();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
dataStream = response.GetResponseStream ();
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
string responseFromServer = reader.ReadToEnd ();
}
}
}
Deserialize Json
Parsing a Json file is equally easy thanks to the DeserializeObject function,
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
Of course, we can obtain the string in any available way. For example, reading it from an existing file.
string path = @"c:\product.json";
using (StreamReader jsonStream = File.OpenText(path))
{
var json = jsonStream.ReadToEnd();
Product product = JsonConvert.DeserializeObject<Product>(json);
}
Or as a response from an Http request
string url = @"http://www.yourJsonUrlAddress.com";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
var json = reader.ReadToEnd();
Product product = JsonConvert.DeserializeObject<Product>(json);
}
These are just some of the many functions available in Json.NET. For more information, consult the examples available on the library’s website.
As we can see, Json.NET makes working with Json files in C# trivial. Undoubtedly a reference library in .NET, and a tool to add to our list of favorites.

