149

Given the following JSON object,

form = {
  "name": "",
  "address": {
    "street": "",
    "city": "",
    "province": "",
    "postalCode": "",
    "country": ""
  },
  "phoneDay": "",
  "phoneCell": "",
  "businessName": "",
  "website": "",
  "email": ""
}

what is a tool to auto-generate the following C# class?

public class ContactInfo
{
    public string Name { get; set; }
    public Address Address { get; set; }
    public string PhoneDay { get; set; }
    public string PhoneCell { get; set; }
    public string BusinessName { get; set; }
    public string Website { get; set; }
    public string Email { get; set; }
}

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string Province { get; set; }
    public string PostalCode { get; set; }
    public string Country { get; set; }
}

We have already looked at these questions:

Generate C# classes from JSON Schema Is asking about JSON Schemas which may be an approach to use down the road.

Benefits and drawbacks of generated C# classes for Json objects

Liam
  • 27,717
  • 28
  • 128
  • 190
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467

3 Answers3

185

Five options:

Pros and Cons:

  • jsonclassgenerator converts to PascalCase but the others do not.

  • app.quicktype.io has some logic to recognize dictionaries and handle JSON properties whose names are invalid c# identifiers.

dbc
  • 104,963
  • 20
  • 228
  • 340
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • both Web Essentials and json2csharp are doing a great job. Didn't try jsonclassgenerator, cause it seemed like a hassle to need to open a separate app just for that – Zoran P. Sep 27 '15 at 06:25
  • 1
    I haven't tried jsonclassgenerator but I can see the advantage. For example if you had a mock api, you could interate over a set of endpoints to regenerate your models. You could go as far as setting a script that checks for new commits and updates the model if anything has changed. You wouldn't want to manually paste new json each time as this could be prone to mistakes. – Billy Jake O'Connor Jan 16 '17 at 15:29
  • The json2csharp tool does a nicer job than VS of formatting the classes especially the class names. It will remove ambiguous characters like "_" and correctly camel case the class names thereafter. – big_water Feb 08 '17 at 17:22
  • 2
    "jsonutils" is pretty cool, it can generate attributes like "Json properties" as well, http://jsonutils.com/ – Dhanuka777 Mar 27 '17 at 00:50
  • app.quicktype.io this one does not work on some machines due to security concerns. – Sean Ch Jun 04 '20 at 10:56
166

Visual Studio 2012 (with ASP.NET and Web Tools 2012.2 RC installed) supports this natively.

Visual Studio 2013 onwards have this built-in.

Visual Studio Paste JSON as Classes screenshot (Image courtesy: robert.muehsig)

Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • 6
    I think this solution is the best, because you will not need an external program! – Abraão Caldas Aug 12 '16 at 18:18
  • before pasting [here](http://superuser.com/questions/777213/copy-json-from-console-log-in-developer-tool-to-clipboard) is **solution to Copy JSON from console.log** in developer tool to clipboard. Hope helps. – Shaiju T Dec 27 '16 at 11:47
12

If you install Web Essentials into Visual studio you can go to Edit => Past special => paste JSON as class.

That is probably the easiest there is.

Web Essentials: http://vswebessentials.com/

Kenneth
  • 28,294
  • 6
  • 61
  • 84