Introduction to Working with JSON in C#
JSON (JavaScript Object Notation) is a lightweight data interchange format that has become the standard for communication between web services, configuration files, and APIs. In the C# (.NET) ecosystem, working with JSON is one of the basic tasks that every developer encounters. In this article, we will thoroughly examine how to perform parsing (deserialization) and serialization of data in C#, using both the built-in System.Text.Json library and the popular third-party library Newtonsoft.Json.
You will learn how to convert C# objects to JSON strings and back, how to work with nested structures, and what pitfalls you may encounter in practice. The material will be useful for both beginners and experienced developers looking to systematize their knowledge.
Serialization of C# Objects to JSON
Serialization is the process of converting a C# object (or other data structure) into a JSON format string. In .NET, there are several approaches for this. Let's look at two main ones: System.Text.Json (recommended by Microsoft) and Newtonsoft.Json (legacy, but still widely used).
Serialization using System.Text.Json
Starting with .NET Core 3.0, Microsoft introduced its own library for working with JSON — System.Text.Json. It is characterized by high performance and minimal memory consumption. The JsonSerializer.Serialize() method is used for serialization.
using System;using System.Text.Json;
public class Person{ public string Name { get; set; } public int Age { get; set; } public string Email { get; set; }}
class Program{ static void Main() { var person = new Person { Name = "Ivan Petrov", Age = 30, Email = "ivan@example.com" };
string json = JsonSerializer.Serialize(person); Console.WriteLine(json); }}Execution result:
{"Name":"Ivan Petrov","Age":30,"Email":"ivan@example.com"}Serialization using Newtonsoft.Json
The Newtonsoft.Json library (also known as Json.NET) was the de facto standard for a long time. It provides more flexibility and settings than the built-in solution. To use it, you need to install the NuGet package Newtonsoft.Json.
using System;using Newtonsoft.Json;
public class Person{ public string Name { get; set; } public int Age { get; set; } public string Email { get; set; }}
class Program{ static void Main() { var person = new Person { Name = "Maria Sidorova", Age = 25, Email = "maria@example.com" };
string json = JsonConvert.SerializeObject(person); Console.WriteLine(json); }}The result will be similar, but Newtonsoft.Json by default uses camelCase notation for property names, if configured.
Serialization Configuration
In both libraries, you can control the output format: ignore null values, use CamelCase, format JSON with indentation, etc.
Example for System.Text.Json with settings:
var options = new JsonSerializerOptions{ WriteIndented = true, // Pretty output with indentation PropertyNamingPolicy = JsonNamingPolicy.CamelCase, // CamelCase notation DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull // Ignore null};
string json = JsonSerializer.Serialize(person, options);Parsing (Deserialization) of JSON in C#
Deserialization is the reverse process: converting a JSON string into a C# object. This is necessary when receiving data from an API, reading configuration files