Quickly Scaffold REST API Model Classes in Visual Studio
Tags: productivity, tutorial, csharp
Published: 2020-06-01 11:17:41

Photo by [Pakata Goh](https://unsplash.com/@pakata?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/programming?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)

---

Picture the following scenario: 

__PM:__
We need to integrate with FooBar's API. 

__You:__
Okay cool, what kind of API is it? REST? GraphQL? gRPC? SOAP? \**shudders*\*

__PM:__
It's a JSON REST API. 

__You:__
Oh wonderful, do they have some Swagger docs?

__PM:__
Not that I know of.

__You:__
Hmm, Postman collection?

__PM:__
Nope.

__You:__
Any documentation?

__PM:__
No, we've been given some endpoints & you'll need to check what they send back.

__You:__
Okay great.

__PM:__
So how long will it take to integrate with their API?

__You:__
Well, there are a lot of unknowns: we don't know what Auth they have on the API, how complex their response models are, or whether the data is even going to be useful or not.

__PM:__
Okay so rough estimate? More than a week? Less than a week?

__You:__
I will need to do some investigation first, can you give me until tomorrow?

__PM:__
Hmm, yeah, is there any way we can bring in the timeline? We need to get this done as soon as possible.

__You:__
![Are you serious face](https://i.kym-cdn.com/photos/images/newsfeed/000/112/480/OpoQQ.jpg?1302279568 "Are you serious?")

Okay, so maybe this tip will not solve all potential issues which may arise in the software development industry, but it may help you save some time when you need to work with a third party API.

## Visual Studio Paste Special to the Rescue

For those of you who already know about this feature, stop reading now, you are already as productive as you can be. :)

But for those of you who don't, this is quite a lesser known feature in Visual Studio not many people are familiar with. I feel it can really help out when you need to create your C# POCO (Plain Old CLR Object) classes for serilializing/deserializing your JSON API payloads - without having to type them all out by hand. Let the computers do the boring, repetitive stuff. Plus, this tip does not require the use of any extensions or third-party utilities.

For example, let's assume we get the following response from an API (taken from the [Working with JSON](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON) page from MDN):

```json
{
  "squadName": "Super hero squad",
  "homeTown": "Metro City",
  "formed": 2016,
  "secretBase": "Super tower",
  "active": true,
  "members": [
    {
      "name": "Molecule Man",
      "age": 29,
      "secretIdentity": "Dan Jukes",
      "powers": [
        "Radiation resistance",
        "Turning tiny",
        "Radiation blast"
      ]
    },
    {
      "name": "Madame Uppercut",
      "age": 39,
      "secretIdentity": "Jane Wilson",
      "powers": [
        "Million tonne punch",
        "Damage resistance",
        "Superhuman reflexes"
      ]
    },
    {
      "name": "Eternal Flame",
      "age": 1000000,
      "secretIdentity": "Unknown",
      "powers": [
        "Immortality",
        "Heat Immunity",
        "Inferno",
        "Teleportation",
        "Interdimensional travel"
      ]
    }
  ]
}
```
Now, in order to be able to use this data in our C# application, we'd need to create a class with properties that correspond to the properties in the JSON response object.

For a simple response like this, it might not take too long. But for an object with 50 properties, different data types & multiple levels of nesting it can get quite tedious.

To quickly scaffold this JSON payload's POCO in Visual Studio, firstly create a new class & remove the class declaration so that only the namespace declaration remains:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2jeexpp8afxykyx3fspi.png)

Then, copy the entire JSON response object to your clipboard, then back in Visual Studio, place your cursor at the location you want to paste, but instead of using a normal paste, navigate to Edit > Paste Special > Paste JSON as Classes.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ub9jrd5lwsiufdjvhata.png)

This will then generate the following classes:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vkbpzw24wum0eygdgks6.png)

The scaffolding tool will then parse the JSON and create properties for each of the objects & try to guess the correct data type for each one. In this case, you can see that it set some properties to `string`, `int` & `bool`. Sometimes it may trip up on things like dates where the format might be a bit weird.

By default, Visual Studio will give the root class a name of `Rootobject` & name the properties the same as the keys in the JSON object, but you are free to change the class & property names to match your naming convention. Just be aware that you may need to configure your JSON serializer appropriately, depending on your case convention.

For the Visual Studio Code users among us, there is an awesome extension which provides much of the same (and more) functionality than the built-in Visual Studio one. Check it out here: [Paste JSON as Code](https://marketplace.visualstudio.com/items?itemName=quicktype.quicktype).

That's pretty much it! It's quite a simple little thing, but I've seen quite a few people that didn't know about this handy little feature. I hope it helps you out & shaves off a bit of your precious time.

Happy coding!