In this tip, you will learn how to convert JSON to XML quickly using Cinchoo ETL framework. It is very simple to use, with few lines of code, the conversion can be done. You can convert large files as the conversion process is stream based, quite fast and with low memory footprint.
ChoETL is an open source ETL (extract, transform and load) framework for .NET. It is a code based library for extracting data from multiple sources, transforming, and loading into your very own data warehouse in .NET environment. You can have data in your data warehouse in no time.
This article talks about generating XML file from JSON format using Cinchoo ETL Framework. It is very simple to use, with few lines of code, the conversion can be done. You can convert large files as the conversion process is stream based, quite fast and with low memory footprint.
This framework library is written in C# using .NET 4.5 / .NET Core 3.x Framework.
3.1 Sample Data
Let's begin by looking into a simple example of converting the below JSON input file.
Listing 3.1.1. Sample Employee JSON Data Input File (emp.json)
[
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
},
{
"firstName": "Tom",
"lastName": "Mark",
"age": 50,
"address": {
"streetAddress": "10 Main Street",
"city": "Edison",
"state": "NJ",
"postalCode": "08837"
},
"phoneNumber": [
{
"type": "home",
"number": "732 555-1234"
},
{
"type": "fax",
"number": "609 555-4567"
}
]
}
]
Expected XML output would be:
[
{
"FirstName": "John",
"LastName": "Doe",
"Street": "120 jefferson st.",
"City": "Riverside",
"State": "NJ",
"Zip": "08075"
},
{
"FirstName": "Jack",
"LastName": "McGinnis",
"Street": "220 hobo Av.",
"City": "Phila",
"State": "PA",
"Zip": "09119"
},
{
"FirstName": "John \"Da Man\"",
"LastName": "Repici",
"Street": "120 Jefferson St.",
"City": "Riverside",
"State": "NJ",
"Zip": "08075"
},
{
"FirstName": "Stephen",
"LastName": "Tyler",
"Street": "7452 Terrace \"At the Plaza\" road",
"City": "SomeTown",
"State": "SD",
"Zip": "91234"
},
{
"FirstName": null,
"LastName": "Blankman",
"Street": null,
"City": "SomeTown",
"State": "SD",
"Zip": "00298"
},
{
"FirstName": "Joan \"the bone\", Anne",
"LastName": "Jet",
"Street": "9th, at Terrace plc",
"City": "Desert City",
"State": "CO",
"Zip": "00123"
}
]
Listing 3.1.2. XML Data Output File (emp.xml)
<Employees>
<Employee>
<firstName>John</firstName>
<lastName>Smith</lastName>
<age>25</age>
<address>
<streetAddress>21 2nd Street</streetAddress>
<city>New York</city>
<state>NY</state>
<postalCode>10021</postalCode>
</address>
<phoneNumber>
<type>home</type>
<number>212 555-1234</number>
</phoneNumber>
<phoneNumber>
<type>fax</type>
<number>646 555-4567</number>
</phoneNumber>
</Employee>
<Employee>
<firstName>Tom</firstName>
<lastName>Mark</lastName>
<age>50</age>
<address>
<streetAddress>10 Main Street</streetAddress>
<city>Edison</city>
<state>NJ</state>
<postalCode>08837</postalCode>
</address>
<phoneNumber>
<type>home</type>
<number>732 555-1234</number>
</phoneNumber>
<phoneNumber>
<type>fax</type>
<number>609 555-4567</number>
</phoneNumber>
</Employee>
</Employees>
The first thing to do is to install ChoETL.JSON /ChoETL.JSON.NETStandard
nuget package. To do this, run the following command in the Package Manager Console.
.NET Framework
Install-Package ChoETL.JSON
.NET Core
Install-Package ChoETL.JSON.NETStandard
Now add ChoETL
namespace to the program.
using ChoETL;
3.2 Quick Conversion
This approach shows how to convert JSON file to XML format with little piece of code. No setup / POCO class are needed.
Listing 3.2.1. Quick JSON to XML File Conversion
private static void QuickConversion()
{
using (var r = new ChoJSONReader("emp.json"))
{
using (var w = new ChoXmlWriter(Console.Out)
.WithRootName("Employees")
.WithNodeName("Employee")
)
{
w.Write(r);
}
}
}
Create an instance of ChoXmlWriter
for producing XML (emp.xml) file. Then create an instance of ChoJSONReader
object for reading emp.json file. VoilĂ , XML file conversion happened with this little piece of code.
Sample fiddle: https://dotnetfiddle.net/QGTzil
3.3 Using POCO Object
This approach shows you how to define POCO entity class and use them for the conversion process. This approach is more type safe and fine control over the conversion process like doing property validation, consuming callback machanism, etc.
Listing 3.3.1. Mapping Class
public class Address
{
public string streetAddress { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postalCode { get; set; }
}
public class PhoneNumber
{
public string type { get; set; }
public string number { get; set; }
}
[ChoXmlRecordObject(XPath = "Emps/Emp")]
public class Employee
{
public string firstName { get; set; }
public string lastName { get; set; }
public int age { get; set; }
public Address address { get; set; }
public List<PhoneNumber> phoneNumber { get; set; }
}
Where use ChoXmlRecordObjectAttribute
to define the XPath to root and element node as shown above.
Then use the Employee
class as below to do the conversion of the file.
Listing 3.3.2. Using POCO Object to Convert JSON to XML File
private static void UsingPOCO()
{
using (var r = new ChoJSONReader<Employee>(json))
{
using (var w = new ChoXmlWriter<Employee>(Console.Out))
{
w.Write(r);
}
}
}
Sample fiddle: https://dotnetfiddle.net/7vNvKF
Download the sample attached above, try it.
For more information about Cinchoo ETL, please visit the below CodeProject article:
History
- 18th October, 2021: Initial version