C# deserializing JSON

Introduction

In this tutorial we will learn how to deserialize JSON in C# using the Json.net library. As we will see in the code below, this library offers a very easy to use API that allows us to deserialize a JSON string to a class object.

As can be seen in figure 1, the library can be easily installed using the NuGet Package Manager, assuming that you are using Visual Studio. We can simply search by “newtonsoft” and install the Nuget in our project.

Installing the C# Json.NET package from the Visual Studio NuGet manager.
Figure 1 – Installing Json.NET from the Visual Studio package manager.

This tutorial was tested with .NET Core 3.1 and with version 12.03 of the Json.net library.

The code

The first thing we are going to do is declaring a new class called Person. This class will represent the JSON we want do desserialize and it is just an example with some fields (name, age and a list of languages the person speaks).

namespace JsonParse
{
    using System.Collections.Generic;

    public class Person
    {
        public Person(string name, int age, List<string> languages)
        {
            Name = name;
            Age = age;
            Languages = languages;
        }

        public string Name { get; }
        
        public int Age { get; }
        
        public List<string> Languages { get; }

    }
}

After this we will move on to the main code. We will start by stating the namespaces we are using. We will use the System namespace, to be able to interact with the console, and the Newtonsoft.Json namespace, to have access to the functionality we need to deserialize the JSON.

using Newtonsoft.Json;
using System;

Moving on to the Main function code, we will define a string that contains our JSON object. The structure of this object should match the same structure we have defined to our Person class. We will assign some arbitrary values to the 3 fields it contains.

var jsonString = @"{
   ""name"": ""Ted"",
   ""age"": 20,
   ""languages"": [""portuguese"", ""spanish""]
}";

Then, to do the deserialization to an object, we simply need to call the DeserializeObject method on the JsonConvert class (this class belongs to the Newtonsoft.Json namespace).

Additionally, we use the Person class as type parameter of the mentioned method. This way, the method will perform the deserialization and return an object of type Person.

var person = JsonConvert.DeserializeObject<Person>(jsonString);

Then we will simply print the values of each field of our obtained class and confirm it matches what was defined on the JSON string.

Console.WriteLine(person.Name);
Console.WriteLine(person.Age);
Console.WriteLine(string.Join(", ", person.Languages));

Just for exemplification, we will now call the deserialization method without using a type parameter. In this case, we will obtain as output an object.

var person2 = JsonConvert.DeserializeObject(jsonString);
Console.WriteLine(person2);

The complete code can be seen below.

namespace JsonParse
{
    using Newtonsoft.Json;
    using System;

    class Program
    {
        static void Main(string[] args)
        {

            var jsonString = @"{
                ""name"": ""Ted"",
                ""age"": 20,
                ""languages"": [""portuguese"", ""spanish""]
            }";

            var person = JsonConvert.DeserializeObject<Person>(jsonString);

            Console.WriteLine(person.Name);
            Console.WriteLine(person.Age);
            Console.WriteLine(string.Join(", ", person.Languages));

            var person2 = JsonConvert.DeserializeObject(jsonString);
            Console.WriteLine(person2);
        }
    }
}

Testing the code

To test the code, simply run it in a tool of your choice. I’m using Visual Studio 2019. You should get an output similar to figure 2.

As can be seen, the 3 initial lines show the 3 properties of the Person object we have obtained. Then we can also see the object we have obtained when we did not set a type parameter, and it also reflects the JSON structure we originally had in the string.

Output of the program.
Figure 2 – Output of the program.

Leave a Reply