Working with JSON Data in C#: Practical Examples

In this guide, we'll explore how to effectively work with JSON data in C#. You'll learn how to serialize and deserialize JSON, manipulate JSON objects, and handle common scenarios using practical examples.
By Jamie

Introduction to JSON in C

JSON (JavaScript Object Notation) is a lightweight data interchange format that’s easy for humans to read and write, and easy for machines to parse and generate. In C#, the System.Text.Json namespace provides powerful tools for working with JSON data.

1. Serializing Objects to JSON

Serialization is the process of converting an object into a JSON string. Here’s how you can serialize a simple C# object:

Example: Serialize a C# Object

using System;
using System.Text.Json;

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

class Program
{
    static void Main()
    {
        Person person = new Person { Name = "Alice", Age = 30 };
        string jsonString = JsonSerializer.Serialize(person);
        Console.WriteLine(jsonString);
    }
}

Output:

{"Name":"Alice","Age":30}

2. Deserializing JSON to Objects

Deserialization is the reverse process where a JSON string is converted back into a C# object. Here’s how to do it:

Example: Deserialize JSON to a C# Object

using System;
using System.Text.Json;

class Program
{
    static void Main()
    {
        string jsonString = "{"Name":"Bob","Age":25}";
        Person person = JsonSerializer.Deserialize<Person>(jsonString);
        Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
    }
}

Output:

Name: Bob, Age: 25

3. Working with JSON Arrays

You can also work with JSON arrays, which are a common structure in JSON data. Let’s see how to handle an array of objects:

Example: Serialize a List of Objects to JSON

using System;
using System.Collections.Generic;
using System.Text.Json;

class Program
{
    static void Main()
    {
        List<Person> people = new List<Person>
        {
            new Person { Name = "Alice", Age = 30 },
            new Person { Name = "Bob", Age = 25 }
        };
        string jsonString = JsonSerializer.Serialize(people);
        Console.WriteLine(jsonString);
    }
}

Output:

[{"Name":"Alice","Age":30},{"Name":"Bob","Age":25}]

4. Manipulating JSON Data

You might need to manipulate JSON data directly. The JsonDocument class allows you to parse and navigate through JSON data without deserializing it into a specific object type.

Example: Read JSON Properties Using JsonDocument

using System;
using System.Text.Json;

class Program
{
    static void Main()
    {
        string jsonString = "{"Name":"Charlie","Age":40}";
        using (JsonDocument doc = JsonDocument.Parse(jsonString))
        {
            JsonElement root = doc.RootElement;
            string name = root.GetProperty("Name").GetString();
            int age = root.GetProperty("Age").GetInt32();
            Console.WriteLine($"Name: {name}, Age: {age}");
        }
    }
}

Output:

Name: Charlie, Age: 40

Conclusion

Working with JSON data in C# is straightforward with the System.Text.Json library. By understanding how to serialize, deserialize, and manipulate JSON data, you can efficiently handle many data interchange scenarios in your applications.