JSON Processing in C# (.NET): Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

JSON Processing in C# (.NET): Complete Guide

JSON (JavaScript Object Notation) is a lightweight data interchange format widely used in web applications, APIs, and configuration files. In the C# (.NET) ecosystem, there are two main approaches to working with JSON: the classic library Newtonsoft.Json (also known as Json.NET) and the modern built-in library System.Text.Json, introduced in .NET Core 3.0 and later. In this article, we will thoroughly examine both tools, their capabilities, syntax, and best practices.



1. Newtonsoft.Json (Json.NET)

The Newtonsoft.Json library has long been the de facto standard for working with JSON in C#. It provides a rich API, LINQ to JSON support, flexible serialization and deserialization configuration. You can install it via NuGet:

Install-Package Newtonsoft.Json


1.1. Serialization and Deserialization

The most basic operations are converting an object to JSON and back. Let's consider the Person class:

public class Person{    public string Name { get; set; }    public int Age { get; set; }    public string Email { get; set; }}


Serialization (object → JSON):

var person = new Person { Name = "Ivan", Age = 30, Email = "ivan@example.com" };string json = JsonConvert.SerializeObject(person, Formatting.Indented);Console.WriteLine(json);


Deserialization (JSON → object):

string json = @"{""Name"":""Maria"",""Age"":25,""Email"":""maria@example.com""}";Person person = JsonConvert.DeserializeObject<Person>(json);Console.WriteLine(person.Name); // Maria


1.2. Working with JObject and JArray

The Newtonsoft.Json library supports LINQ to JSON, allowing you to dynamically access JSON elements without creating strongly typed classes.

string json = @"{""products"":[{""id"":1,""name"":""Laptop""},{""id"":2,""name"":""Mouse""}]}";JObject obj = JObject.Parse(json);JArray products = (JArray)obj["products"];

foreach (var product in products){ int id = (int)product["id"]; string name = (string)product["name"]; Console.WriteLine($"ID: {id}, Name: {name}");}


1.3. Attributes for Serialization Configuration

Using attributes, you can control property names in JSON, ignore fields, set default values, and much more.

public class User{    [JsonProperty("user_name")]    public string Name { get; set; }

[JsonIgnore] public string Password { get; set; }

[JsonProperty("registration_date")] public DateTime CreatedAt { get; set; }}

// During serialization, Password will be skipped, and Name will become "user_name"string json = JsonConvert.SerializeObject(user);


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

Starting with .NET Core 3.0, Microsoft introduced its own library System.Text.Json, optimized for performance and minimal memory consumption. It does not require installing additional packages (except for some extensions).



2.1. Serialization and Deserialization

Working with System.Text.Json is similar to Newtonsoft.Json, but has some differences in syntax and default behavior (e.g., case sensitivity).

using System.Text.Json;

var person = new Person { Name = "Alexey", Age = 28, Email = "alex@example.com" };string json = JsonSerializer.Serialize(person, new JsonSerializerOptions { WriteIndented = true });Console.WriteLine(json);

// Deserializationstring jsonInput = @"{""Name"":""Olga"",""Age"":32,""Email"":""olga@example.com""}";Person deserializedPerson = JsonSerializer.Deserialize<Person>(jsonInput);


Important! By default, System.Text.Json i

Recommendations