In the previous article, I mentioned TRIM(), LTRIM(), RTRIM() Functions in SQL Server and Nested and Complex JSON examples but in this article, I have mentioned how to Serialize List to JSON in C# with a console application example in .NET and .NET Core.

Serialize List to JSON in C# (.NET Framework)

Suppose, we have already created a console application in .NET 4.7 in Visual Studio, now, we will first need to install 'Newtonsoft.Json', which we can do using the below Package manager console (Tools -> Nuget package manager -> Package Manager Console)

Install-Package Newtonsoft.Json

Once we have installed the Nuget package using above command.

Then create a new class, as shown below, which we will use in List to convert into JSON string.

 public class UserDetail
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

Now, in our console application, we will add a few objects in List<UserDetail>, and then convert them into JSON string

            List<UserDetail> users = new List<UserDetail>();
            users.Add(new UserDetail
            {
                Id = 1,
                Name = "John",
            });

            users.Add(new UserDetail
            {
                Id = 2,
                Name = "Smith",
            });

            users.Add(new UserDetail
            {
                Id = 3,
                Name = "Glenn",
            });

            var Json = JsonConvert.SerializeObject(users); //serialize list to JSON
 
            //print json
            Console.WriteLine(Json);
            Console.ReadLine();

Here is the complete console application code

using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace SerializeListToJSON
{
    public class Program
    {
        static void Main(string[] args)
        {
            List<UserDetail> users = new List<UserDetail>();
            users.Add(new UserDetail
            {
                Id = 1,
                Name = "John",
            });

            users.Add(new UserDetail
            {
                Id = 2,
                Name = "Smith",
            });

            users.Add(new UserDetail
            {
                Id = 3,
                Name = "Glenn",
            });

            var Json = JsonConvert.SerializeObject(users);

            Console.WriteLine(Json);
            Console.ReadLine();
        }
    }

    public class UserDetail
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

Output:

serialize-list-to-json-csharp

To Deserialize the above JSON into a List using C#, you can simply use the below code

List<UserDetail> userList = JsonConvert.DeserializeObject<List<UserDetail>>(Json);

Serialize List to JSON in .NET Core C#

Considering the above example, we don't have to install the NewtonSoft.JSON package in .NET Core 3 or above, since it has System.Text.JSON which can be used to Serialize List to JSON

So, in .NET Core, we use below code

using System;
using System.Collections.Generic;
using System.Text.Json; //added this reference

namespace ListToJSON
{
    public class Program
    {
        static void Main(string[] args)
        {
            List<UserDetail> users = new List<UserDetail>();
            users.Add(new UserDetail
            {
                Id = 1,
                Name = "John",
            });

            users.Add(new UserDetail
            {
                Id = 2,
                Name = "Smith",
            });

            users.Add(new UserDetail
            {
                Id = 3,
                Name = "Glenn",
            });

            //this is new line
            var json = JsonSerializer.Serialize(users);

            Console.WriteLine(json);

            Console.ReadLine();
        }
    }
    public class UserDetail
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

Output will be same as above.

Basically, we have this different line than .NET framework code

var json = JsonSerializer.Serialize(users);

and we can simply use 'using System.Text.Json;' namespace to serialize list to JSON in C# .NET Core.

That's it.

You may also like to read:

Read JSON using C# .NET or .NET Core

Read JSON using Javascript