🌤

Create a class from a JSON in Visual Studio 2022

visualstudio2022vs2022productivitycsharp
Loading...
 

Published on

April 29, 2022

It happens very often, during the development, to have some objects that are returned by some API and of which we want to create a class. If the object are big with a lot of subnodes, it should be a long activity and, first of all, very annoying.

The Visual Studio team improved this feature in the last version of the IDE.

How to use this feature

Create an empty class or just a file with the namespace declaration.

an empty file

Then you can copy your JSON into the clipboard and in Visual Studio go to Edit -> Paste Special -> Paste JSON as Classes

Paste special menu

This feature is very fast right now. With the previous version of Visual Studio, sometimes, you had issues to create a class from a big JSON file.

Keyboard Shortcut

By default this command is not associated to any keyboard shortcut. In my environment I configured that from the menu Tools -> Options.

Paste as Class keyboard shortcut

I choosen Ctrl+V, Ctrl+J but you can choose your favourite shortcut for this special action.

React, comment and follow on