JSON Processing in C# (.NET): Complete Guide
JSON (JavaScript Object Notation) is a lightweight data interchange format that has become the standard for web APIs and configuration files. In the C# (.NET) ecosystem, there are two main tools for working with JSON: the time-tested library Newtonsoft.Json (also known as Json.NET) and the modern built-in library System.Text.Json, introduced in .NET Core 3.0. In this article, we will thoroughly examine both libraries, their capabilities, and usage examples.
1. Newtonsoft.Json (Json.NET)
Newtonsoft.Json has been the de facto standard for working with JSON in C# for many years. It offers rich functionality, including flexible serialization, support for LINQ to JSON, and work with dynamic types.
Installation
To install via NuGet Package Manager, run the command:
Install-Package Newtonsoft.JsonOr via .NET CLI:
dotnet add package Newtonsoft.JsonBasic Operations: Serialization and Deserialization
Serialization is the conversion of a C# object to a JSON string. Deserialization is the reverse process.
using Newtonsoft.Json;
public class Person{ public string Name { get; set; } public int Age { get; set; } public List<string> Hobbies { get; set; }}
// SerializationPerson person = new Person{ Name = "Alexey", Age = 30, Hobbies = new List<string> { "Programming", "Reading", "Traveling" }};
string json = JsonConvert.SerializeObject(person, Formatting.Indented);Console.WriteLine(json);
// Deserializationstring jsonInput = @"{'Name':'Maria','Age':25,'Hobbies':['Drawing','Yoga']}";Person deserializedPerson = JsonConvert.DeserializeObject<Person>(jsonInput);Console.WriteLine($"Name: {deserializedPerson.Name}, Age: {deserializedPerson.Age}");Working with Dynamic Data
Newtonsoft.Json supports dynamic types, which is convenient when working with an unknown JSON structure.
dynamic dynamicObject = JsonConvert.DeserializeObject("{'Name':'Ivan','Score':95.5}");Console.WriteLine($"Student: {dynamicObject.Name}, Score: {dynamicObject.Score}");LINQ to JSON
The library provides a powerful API for querying JSON data using LINQ.
using Newtonsoft.Json.Linq;
string json = @"{ 'users': [ {'name': 'Anna', 'age': 28}, {'name': 'Peter', 'age': 35}, {'name': 'Elena', 'age': 22} ]}";
JObject obj = JObject.Parse(json);var youngUsers = obj["users"] .Where(u => (int)u["age"] < 30) .Select(u => (string)u["name"]);
foreach (var name in youngUsers){ Console.WriteLine(name); // Output: Anna, Elena}2. System.Text.Json
System.Text.Json is a modern library from Microsoft, optimized for performance and minimal memory consumption. It is built into .NET and does not require installing additional packages.
Basic Operations
using System.Text.Json;using System.Text.Json.Serialization;
public class Product{ [JsonPropertyName("product_name")] public string Name { get; set; } public decimal Price { get; set; } [JsonIgnore] public string InternalCode { get; set; }}
// SerializationProduct product = new Product{ Name = "Laptop", Price = 75000.50m, InternalCode = "INT-001"};
string json = JsonSerializer.Serialize(product, new JsonSerializerOptions { WriteIndented = true });Console.WriteLine(json);
// Deserializationstring jsonInput = @"{""product_name"":""Smartphone"",""Price"":45000.00}";Product deserializedProduct = JsonSerializer.Deserialize<Product>(jsonInput);Console.WriteLine($"Product: {deserializedProduct.Name}, Price: {deserializedProduct.Price}"); Also in library
Elasticsearch client for C# (.NET): NEST and Elastic.Clients.Elasticsearch
CLI Frameworks for PHP: Creating Console Applications with Symfony Console and Laravel Zero
Elasticsearch client for PHP: elasticsearch-php — complete guide