Simplifying Class Generation from JSON and XML with json2csharp.com or Visual Studio Paste Special
11 Jun 2024 · Rabington Chitima
Back again on the simple things that can make software development better and efficient. Today I'll cover converting JSON and XML data to class structures.
It's hard to be in software development and not be involved with XML or JSON data one way or another — in most instances they're the backbone for your REST or SOAP system integrations. The common task we can agree on is the need to convert JSON or XML data into class structures for your logical needs. Whether you are working on a web application, a mobile app, or any other software project, representing data in a structured format is crucial for efficient processing and manipulation. Fortunately, there are tools and features available that streamline this process, saving developers time and effort. Here we'll discuss the two I make use of most: 1) json2csharp.com and 2) Visual Studio Paste Special.
1. Using json2csharp.com
json2csharp.com is a web-based utility designed to convert JSON or XML files into corresponding C# / Java / Python / Dart class definitions. It simplifies the creation of class structures by automatically analysing the provided data and generating the necessary code, intelligently naming classes and properties to closely match the original structure.
You paste your JSON or XML data directly into the provided text area, and on submission the tool parses the input and generates class definitions. For instance, consider a JSON object representing information about a person:
{
"name": "Rabington Chitima",
"email": "[email protected]",
"profession": "Software Engineer"
}
If you selected Java, you automatically get:
public class Root {
public String name;
public String email;
public String profession;
}
If you selected C#, you automatically get:
public class Root
{
public string name { get; set; }
public string email { get; set; }
public string profession { get; set; }
}
This generated class can then be used within your Java or C# codebase to deserialize JSON data into strongly-typed objects, simplifying data manipulation and enhancing code readability. This doesn't save much time on a 3-line object, but the value shows once you're working with a large, real-world JSON or XML payload — for example integration documentation such as an OASIS UBL invoice XML sample or the Sage Accounting API setup reference.
2. Using Visual Studio Paste Special
While json2csharp.com offers a convenient web-based solution, developers often prefer tools integrated directly into their development environment. Visual Studio provides a feature called "Paste Special" that offers similar functionality.
To use Paste Special in Visual Studio:
- Copy the JSON or XML data you want to convert to classes.
- Access "Paste Special" — right-click on the target file or project in Solution Explorer, then navigate to Paste Special → Paste JSON As Classes or Paste XML As Classes, depending on the data format. (If you can't find it on right-click, look under Edit on the toolbar.)
Sample XML:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
</book>
</catalog>
And the generated Paste Special output:
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class catalog
{
private catalogBook[] bookField;
[System.Xml.Serialization.XmlElementAttribute("book")]
public catalogBook[] book
{
get { return this.bookField; }
set { this.bookField = value; }
}
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class catalogBook
{
private string authorField;
private string titleField;
private string genreField;
private decimal priceField;
private System.DateTime publish_dateField;
private string descriptionField;
private string idField;
public string author
{
get { return this.authorField; }
set { this.authorField = value; }
}
public string title
{
get { return this.titleField; }
set { this.titleField = value; }
}
public string genre
{
get { return this.genreField; }
set { this.genreField = value; }
}
public decimal price
{
get { return this.priceField; }
set { this.priceField = value; }
}
[System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
public System.DateTime publish_date
{
get { return this.publish_dateField; }
set { this.publish_dateField = value; }
}
public string description
{
get { return this.descriptionField; }
set { this.descriptionField = value; }
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public string id
{
get { return this.idField; }
set { this.idField = value; }
}
}
Conclusion
As you work with your XML and JSON files, either approach can give you your class properties with a few clicks rather than typing everything by hand. Happy coding!