Visual Studio : How to convert JSON to a C# class using Paste Special

Visual Studio - how to create model class from JSON object in one single step

Recently, I wanted to create a C# project where I would process the JSON response from the cryptocurrency-related API endpoints. I was wondering what was the best way to quickly create a strongly typed model class from a JSON object and if there is a way to generate this automatically from the JSON data instead of writing the model class manually. It turns out there is and in this article, we are going to examine two solutions for this problem.

Using CoindDesk JSON API result as an example

To demonstrate the generated code produced by the two solutions, we are going to use the Cryptocurrency news site CoinDesk that provides simple API for Bitcoin price. We are going to use the following endpoint:

https://api.coindesk.com/v1/bpi/currentprice.json

Here is the JSON response of that endpoint:

{"time":{"updated":"Jul 10, 2020 07:59:00 UTC","updatedISO":"2020-07-10T07:59:00+00:00","updateduk":"Jul 10, 2020 at 08:59 BST"},"disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org","chartName":"Bitcoin","bpi":{"USD":{"code":"USD","symbol":"$","rate":"9,157.1832","description":"United States Dollar","rate_float":9157.1832},"GBP":{"code":"GBP","symbol":"£","rate":"7,275.8765","description":"British Pound Sterling","rate_float":7275.8765},"EUR":{"code":"EUR","symbol":"€","rate":"8,124.4727","description":"Euro","rate_float":8124.4727}}}

We are going to examine two different ways to create a strongly-typed C# class from the output shown above.

Note: Visual Studio 2019 Community Edition was used when writing this article.

Solution 1 - using Visual Studio Paste Special option

Visual Studio IDE does have this tool available, but it is sort of a hidden feature. In the menu, it should be available at Edit > Paste Special, but for Paste Special option to appear in Visual Studio, the currently opened file needs to be either .cs or a .vb file type.

Note: If you are still unable to find the "Paste Special" option after going through the steps below or if your previous version of Visual Studio had it, but now the Paste Special option is gone from the menu, your Visual Studio installation might be missing a component. We will discuss this in the next section.

The steps to enable Past Special in VS are as follows:

  1. First, you need to open a new C# class file In Visual Studio. One way to do that is to select File > New File in the menu.
  2. A "New File" window will open. From the list, select the Visual C# Class file and click Open.
  3. From the newly created C# file, delete all the auto-generated code so that it will be empty.
  4. Copy the JSON data into the clipboard.
  5. Go back to Visual Studio. Now, the Paste Special should be available in the menu at Edit > Paste Special > Paste JSON as Classes:
    Visual Studio - Paste Special option in menu to convert JSON data to C# class
Note: Make sure that the clipboard contains a valid JSON data when you do Paste Special. If it doesn't, the VS will give you the following error message: "The content in the clipboard isn't a valid JSON instance. Please fix the issue and try again.".

After going through all the steps, the C# file should now contain an auto-generated model class which in our case is Bitcoin Price JSON response data from the CoinDesk API URL. The class created should look like this:

public class Rootobject
{
    public Time time { get; set; }
    public string disclaimer { get; set; }
    public string chartName { get; set; }
    public Bpi bpi { get; set; }
}

public class Time
{
    public string updated { get; set; }
    public DateTime updatedISO { get; set; }
    public string updateduk { get; set; }
}

public class Bpi
{
    public USD USD { get; set; }
    public GBP GBP { get; set; }
    public EUR EUR { get; set; }
}

public class USD
{
    public string code { get; set; }
    public string symbol { get; set; }
    public string rate { get; set; }
    public string description { get; set; }
    public float rate_float { get; set; }
}

public class GBP
{
    public string code { get; set; }
    public string symbol { get; set; }
    public string rate { get; set; }
    public string description { get; set; }
    public float rate_float { get; set; }
}

public class EUR
{
    public string code { get; set; }
    public string symbol { get; set; }
    public string rate { get; set; }
    public string description { get; set; }
    public float rate_float { get; set; }
}

What if Paste Special Tool is still missing in Visual Studio

If you still can't locate the Paste Special tool in Visual Studio, it usually means that the required component was not installed during VS installation.

It seems the Visual Studio needs the "Web Development Tools plus .NET Core 2.1" component. This is added when you install the ASP.NET and web development workload, but installing only the Web Development Tools plus .NET Core 2.1 component should be enough.

So we need to modify the Visual Studio. There are different ways to accomplish that, one way is as follows:

  1. Run the Visual Studio. A "Get Started" window will show up. Select "Create a new project".
  2. "Create new project" window will show up. Scroll through all the available templates all way to the end. There should be a "Install more tools and features" link at the bottom as shown below. Click on it.
    Visual Studio - create new project window - install more tools and features link
  3. The Visual Studio Installer will open. Select the "Individual components" tab.
    Visual Studio Installer - Individual components tab
  4. in the search field, type "web". The Web Development Tools plus .NET Core 2.1 component should show up under .NET.
    Visual Studio Installer - Individual components tab - search web
  5. Select it and click the Modify button.

Solution 2 - using an online converter tool

If you still have trouble enabling the Paste Special option in Visual Studio or if you are just looking for the online solution, there is an online tool json2csharp.com available that converts the JSON object to a C# class. It took only a second or two with JSON2Csharp tool to get the following C# class from the JSON object:

// Root myDeserializedClass = JsonConvert.DeserializeObject(myJsonResponse); 
    public class Time    {
        public string updated { get; set; } 
        public DateTime updatedISO { get; set; } 
        public string updateduk { get; set; } 

    }

    public class USD    {
        public string code { get; set; } 
        public string symbol { get; set; } 
        public string rate { get; set; } 
        public string description { get; set; } 
        public double rate_float { get; set; } 

    }

    public class GBP    {
        public string code { get; set; } 
        public string symbol { get; set; } 
        public string rate { get; set; } 
        public string description { get; set; } 
        public double rate_float { get; set; } 

    }

    public class EUR    {
        public string code { get; set; } 
        public string symbol { get; set; } 
        public string rate { get; set; } 
        public string description { get; set; } 
        public double rate_float { get; set; } 

    }

    public class Bpi    {
        public USD USD { get; set; } 
        public GBP GBP { get; set; } 
        public EUR EUR { get; set; } 

    }

    public class Root    {
        public Time time { get; set; } 
        public string disclaimer { get; set; } 
        public string chartName { get; set; } 
        public Bpi bpi { get; set; } 

    }

As you can see, this online tool created an almost identical model class from JSON object as the Paste Special option in Visual Studio. The only difference is the name of the Root object and that it used double type instead of float.

Conclusion

When we need to create a model class from the JSON object, we don't need to write them manually. There is a "Paste Special" option available in Visual Studio, but it does take a bit of effort to locate it. We also have an option to use an online tool that creates more or less the same model class from JSON data compared to Visual Studio.

Write a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.