Introduction to Working with JSON in C#
\n\nJSON (JavaScript Object Notation) is a lightweight data interchange format widely used in web development, mobile applications, and microservices. For a C# developer, the ability to work with JSON is an essential skill. In this article, we will cover the basics of parsing (deserialization) and serialization of JSON using the modern namespace System.Text.Json, which is included in .NET Core 3.0 and later, as well as .NET 5/6/7/8.
We will cover:
\n- \n
- What serialization and deserialization are \n
- How to convert a C# object to a JSON string \n
- How to read JSON and convert it back to an object \n
- Working with nested objects and collections \n
- Error handling and serialization configuration \n
All code examples are written for a C# console application (.NET 6+). You can copy them and run them immediately.
\n\n1. Serialization Basics: Object → JSON
\n\nSerialization is the process of converting an object (or data structure) into a JSON format string. In C#, the JsonSerializer class from the System.Text.Json namespace is used for this.
Creating a Simple Model
\n\nSuppose we have a Person class:
public class Person\n{\n public string Name { get; set; }\n public int Age { get; set; }\n public string Email { get; set; }\n}\n\nSerializing an Object
\n\nNow let's create an instance and convert it to JSON:
\n\nusing System.Text.Json;\n\nPerson person = new Person\n{\n Name = "Ivan Petrov",\n Age = 30,\n Email = "ivan@example.com"\n};\n\nstring jsonString = JsonSerializer.Serialize(person);\nConsole.WriteLine(jsonString);\n\nResult:
\n\n{"Name":"Ivan Petrov","Age":30,"Email":"ivan@example.com"}\n\nBy default, JsonSerializer uses camelCase for property names (first letter lowercase), but in our example the names remained capitalized because we did not change the settings. If you need strict camelCase, use JsonSerializerOptions:
var options = new JsonSerializerOptions\n{\n PropertyNamingPolicy = JsonNamingPolicy.CamelCase\n};\nstring jsonCamelCase = JsonSerializer.Serialize(person, options);\nConsole.WriteLine(jsonCamelCase);\n// {"name":"Ivan Petrov","age":30,"email":"ivan@example.com"}\n\n2. Deserialization: JSON → Object
\n\nDeserialization is the reverse process: we take a JSON string and convert it into a C# object.
\n\nSimple Deserialization Example
\n\nSuppose we received JSON from an API:
\n\nstring jsonFromApi = @"{""Name"":""Anna Smirnova"",""Age"":25,""Email"":""anna@test.com""}";\n\nPerson? personFromJson = JsonSerializer.Deserialize<Person>(jsonFromApi);\n\nif (personFromJson != null)\n{\n Console.WriteLine($"Name: {personFromJson.Name}, Age: {personFromJson.Age}");\n}\n\nNote the ? sign after the Person type — this is a nullable annotation indicating that the result can be null (for example, if the JSON is invalid).
Working with Arrays and Lists
\n\nOften JSON contains collections of objects. Suppose we have an array of users:
\n\nstring jsonArray = @"\n[\n {""Name"":""Oleg"",""Age"":35},\n {""Name"":""Maria"",""Age"":28},\n {""Name"":""Dmitry"",""Age"":42}\n]";\n\nList<Person>? people = JsonSerializer.Deserialize<List<Person>>(jsonArray);\n\nif (people != null)\n{\n foreach (var p in people)\n {\n Console.WriteLine($"{p.Name} - {p.Age} years old");\n }\n}\n\n3. Working with Nested Objects and Complex Structures
\n\nReal JSON data is rarely flat. Let's consider