Convert Json to classes

In the world of software development, working with JSON data is a common task. JSON (JavaScript Object Notation) is a popular data format used for storing and exchanging structured information. When integrating JSON data into our projects, one crucial step is to convert the JSON structure into classes that can be easily manipulated and utilized within our code. However, manually converting JSON to classes can be a tedious and error-prone process, especially for beginners. That’s where Visual Studio comes to the rescue with a powerful shortcut that many developers are unaware of. In this blog post, we will explore how this feature in Visual Studio can save us from the headache of manually converting JSON to classes and significantly simplify our development process.

convert json to classes

Step 1: Create a new project in Visual Studio

To get started, open Visual Studio and create a new project. Choose the appropriate project template based on your requirements, such as a Console Application or a Class Library. This will serve as the container for the classes we generate from the JSON data.

Step 2: Create a .cs class file

Inside your newly created project, add a new C# class file where the generated classes will reside. Right-click on the project in the Solution Explorer, select “Add,” and then choose “Class.”

Give the class file an appropriate name, such as “JsonClasses.cs”. Remove all the pre-generated content from file. 

Step 3: Copy the JSON data

Before we can generate classes, we need to have the JSON data available. This data can come from various sources, such as an API response or a sample JSON file. Copy the JSON data that you want to convert to classes. In my case, I have got the json from Google Places API response.

here is example Json that I am using

{
  "html_attributions": [],
  "results": [
    {
      "geometry": {
        "location": {
          "lat": 37.7749,
          "lng": -122.4194
        },
        "viewport": {
          "northeast": {
            "lat": 37.77636282989273,
            "lng": -122.4179338701073
          },
          "southwest": {
            "lat": 37.77366317010728,
            "lng": -122.4206335298927
          }
        }
      },
      "icon": "https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/lodging-71.png",
      "id": "1234567890abcdefg",
      "name": "Example Hotel",
      "opening_hours": {
        "open_now": true
      },
      "photos": [
        {
          "height": 3024,
          "html_attributions": [
            "<a href=\"https://example.com\">Example User</a>"
          ],
          "photo_reference": "ABC123",
          "width": 4032
        }
      ],
      "place_id": "ChIJd8BlQ2BZwokRAFUEcm_qrcA",
      "plus_code": {
        "compound_code": "QHGR+JF San Francisco, California, United States",
        "global_code": "849VQHGR+JF"
      },
      "rating": 4.5,
      "reference": "ChIJd8BlQ2BZwokRAFUEcm_qrcA",
      "types": ["lodging", "point_of_interest", "establishment"],
      "user_ratings_total": 100,
      "vicinity": "123 Main Street, San Francisco"
    },
    // Additional results...
  ],
  "status": "OK"
}

Step 4: Use “Paste JSON as Classes” feature

In Visual Studio, go to the “Edit” menu and select “Paste Special”. If you don’t see the “Paste JSON as Classes” option, you might need to install the corresponding extension from the Visual Studio Marketplace. Once you have the option available, click on “Paste JSON as Classes”. If you find your option disabled, Copy again your Json Data , and make sure the empty .cs file you recently created is open and cursor is in that file.

Step 5: Generate the classes from json

All classes structure based on the JSON data you copied will be created in your .cs file. Visual Studio will automatically generate the classes, including properties and nested objects, reflecting the structure of the JSON. The generated classes will be pasted into the class file you created earlier.

Here are the classes reflecting above json we used as Example.


public class Rootobject
{
    public object[] html_attributions { get; set; }
    public Result[] results { get; set; }
    public string status { get; set; }
}

public class Result
{
    public Geometry geometry { get; set; }
    public string icon { get; set; }
    public string id { get; set; }
    public string name { get; set; }
    public Opening_Hours opening_hours { get; set; }
    public Photo[] photos { get; set; }
    public string place_id { get; set; }
    public Plus_Code plus_code { get; set; }
    public float rating { get; set; }
    public string reference { get; set; }
    public string[] types { get; set; }
    public int user_ratings_total { get; set; }
    public string vicinity { get; set; }
}

public class Geometry
{
    public Location location { get; set; }
    public Viewport viewport { get; set; }
}

public class Location
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Viewport
{
    public Northeast northeast { get; set; }
    public Southwest southwest { get; set; }
}

public class Northeast
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Southwest
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Opening_Hours
{
    public bool open_now { get; set; }
}

public class Plus_Code
{
    public string compound_code { get; set; }
    public string global_code { get; set; }
}

public class Photo
{
    public int height { get; set; }
    public string[] html_attributions { get; set; }
    public string photo_reference { get; set; }
    public int width { get; set; }
}

Congratulations, you are done

A Bonus: you might have noticed xml to classes option on the sample place 🙂

It’s worth mentioning that there are also online tools available that can generate classes from JSON or XML data if you prefer to use them outside of Visual Studio. Some popular tools include json2csharp.com for JSON and xmltocsharp.azurewebsites.net for XML.

Leave a Comment