157

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

6
  • 5
    json2csharp.com ??? Commented Feb 6, 2014 at 19:03
  • try this tool might help.. tools4geeks.com/json-to-Csharp-classes Commented Jul 18, 2016 at 21:25
  • Anyone know how to do this same thing with Swagger YAML? Commented Mar 13, 2018 at 19:29
  • Unfortunately json2charp.com is now a domain on sale. That sucks, and the quicktipe one is kinda clunky. Does anybody know a better alternative? Commented Apr 23, 2020 at 21:50
  • @DARKGuy jsonutils jsonutils.com Commented May 27, 2020 at 19:05

3 Answers 3

191

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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
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.
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.
"jsonutils" is pretty cool, it can generate attributes like "Json properties" as well, jsonutils.com
app.quicktype.io this one does not work on some machines due to security concerns.
183

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)

2 Comments

I think this solution is the best, because you will not need an external program!
before pasting here is solution to Copy JSON from console.log in developer tool to clipboard. Hope helps.
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/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.