Click here to Skip to main content
15,125,746 members
Articles / Programming Languages / XML
Tip/Trick
Posted 18 Oct 2021

Stats

2.3K views
4 bookmarked

Cinchoo ETL - Quick Start: Converting JSON to Xml File

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
18 Oct 2021CPOL2 min read
Quick tutorial about converting JSON to Xml file using Cinchoo ETL
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.

1. Introduction

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.

2. Requirement

This framework library is written in C# using .NET 4.5 / .NET Core 3.x Framework.

3. How to Use

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.

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
JavaScript
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
JavaScript
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
JavaScript
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:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Cinchoo
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA19-Oct-21 15:32
professionalȘtefan-Mihai MOGA19-Oct-21 15:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.