JSON Processing in C# (.NET): Complete Guide with Examples
JSON (JavaScript Object Notation) is one of the most popular data exchange formats in modern web applications, APIs, and configuration files. In the .NET ecosystem, developers have access to powerful tools for working with JSON, allowing easy serialization of objects to JSON and back. In this article, we will examine two main approaches: the built-in System.Text.Json library (recommended for .NET Core 3.0+ and .NET 5+) and the popular third-party library Newtonsoft.Json (also known as Json.NET).
1. System.Text.Json — Modern .NET Standard
Starting with .NET Core 3.0, Microsoft introduced its own high-performance library for working with JSON — System.Text.Json. It is optimized for minimal memory consumption and high speed, making it an ideal choice for new projects.
1.1. Installation and Import
The library is already built into .NET 5/6/7/8/9, so no additional installation is required. To use it, simply include the namespace:
using System.Text.Json;using System.Text.Json.Serialization;1.2. Basic Serialization and Deserialization
Let's look at a simple example: serializing an object to JSON and back.
// Define a classpublic class Person{ public string Name { get; set; } public int Age { get; set; } public bool IsEmployed { get; set; }}
// Serializationvar person = new Person { Name = "Ivan", Age = 30, IsEmployed = true };string json = JsonSerializer.Serialize(person);Console.WriteLine(json); // {"Name":"Ivan","Age":30,"IsEmployed":true}
// Deserializationvar deserializedPerson = JsonSerializer.Deserialize<Person>(json);Console.WriteLine(deserializedPerson.Name); // Ivan1.3. Working with Attributes
System.Text.Json supports attributes for fine-tuning serialization:
public class Product{ [JsonPropertyName("product_id")] public int Id { get; set; } [JsonIgnore] public string InternalCode { get; set; } [JsonConverter(typeof(JsonStringEnumConverter))] public ProductCategory Category { get; set; }}
public enum ProductCategory{ Electronics, Clothing, Food}
var product = new Product { Id = 101, InternalCode = "SECRET", Category = ProductCategory.Electronics };
string json = JsonSerializer.Serialize(product);// {"product_id":101,"Category":"Electronics"}// InternalCode did not make it into JSON thanks to [JsonIgnore]1.4. Serialization Options
You can configure the serializer's behavior through JsonSerializerOptions:
var options = new JsonSerializerOptions{ WriteIndented = true, // Pretty output with indentation PropertyNamingPolicy = JsonNamingPolicy.CamelCase, // camelCase for properties DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull // Ignore null};
string prettyJson = JsonSerializer.Serialize(person, options);Console.WriteLine(prettyJson);/* Output:{ "name": "Ivan", "age": 30, "isEmployed": true}*/2. Newtonsoft.Json (Json.NET) — Time-Tested Tool
Before the advent of System.Text.Json, Newtonsoft.Json was the de facto standard for working with JSON in .NET. It offers more flexible settings and a wide range of features, although it lags in performance.
2.1. Installation
To use Newtonsoft.Json, you need to install the NuGet package:
// Via Package Manager Console:Install-Package Newtonsoft.Json
// Or via .NET CLI:dotnet add package Newtonsoft.Json2.2. Basic Usage
using Newtonsoft.Json;
public class Or Also in library
Working with CSV in TypeScript: Libraries and Code Examples
Data Validation in Go (Golang): Libraries, Approaches, and Code Examples
Working with ZIP Archives in Rust: zip Library and Complete Guide
Working with CSV in Java: A Complete Guide to Reading and Writing
Geolocation and Maps in C# (.NET): Overview of Libraries and Tools