Working with JSON in C#: Parsing and Serialization for Beginners

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

Introduction to Working with JSON in C#

\n\n

JSON (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.

\n\n

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
\n\n

All code examples are written for a C# console application (.NET 6+). You can copy them and run them immediately.

\n\n

1. Serialization Basics: Object → JSON

\n\n

Serialization 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.

\n\n

Creating a Simple Model

\n\n

Suppose we have a Person class:

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

Serializing an Object

\n\n

Now let's create an instance and convert it to JSON:

\n\n
using 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\n

Result:

\n\n
{"Name":"Ivan Petrov","Age":30,"Email":"ivan@example.com"}
\n\n

By 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:

\n\n
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\n

2. Deserialization: JSON → Object

\n\n

Deserialization is the reverse process: we take a JSON string and convert it into a C# object.

\n\n

Simple Deserialization Example

\n\n

Suppose we received JSON from an API:

\n\n
string 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\n

Note 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).

\n\n

Working with Arrays and Lists

\n\n

Often JSON contains collections of objects. Suppose we have an array of users:

\n\n
string 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\n

3. Working with Nested Objects and Complex Structures

\n\n

Real JSON data is rarely flat. Let's consider

Blogs

Book Recommendations