TIL: Generate C# classes out of JSON or XML in Visual Studio 2015

You can quickly generate C# classes out of JSON or XML markup in Visual Studio 2015. This might come in handy if you have a response from a REST API in JSON format and want to work with it in your C# application.

Note: This option is not available in Visual Studio Code. If you are using it you might want to use online services available.

The process is straightforward: Copy the source JSON or XML into the clipboard. Then in Visual Studio, select Edit->Paste Special->Paste JSON as Classes:

TIL - paste json as classes

Classes declarations will be inserted into the currently opened file. Additional refactoring is usually needed after that, but it will get you started.

As an example, let’s consider the following JSON:

    "employees": [
        { "firstName": "John", "lastName": "Doe" },
        { "firstName": "Anna", "lastName": "Smith" },
        { "firstName": "Peter", "lastName": "Jones " }

Visual Studio will generate the following C# code:

public class Rootobject
    public Employee[] employees { get; set; }

public class Employee
    public string firstName { get; set; }
    public string lastName { get; set; }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s