Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / XML
Tip/Trick

Cinchoo ETL - Converting JSON to XML with Namespaces

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
27 Oct 2021CPOL3 min read 4.3K   1  
Tip to convert JSON to XML with namespaces using Cinchoo ETL
In this tip, you will learn how to convert JSON to XML with namespaces using Cinchoo ETL framework. It is very simple to use, with few lines of code, the conversion can be done. You can perform such conversion process as 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 tip talks about converting JSON to XML with namespaces 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 the below JSON file containing complex, nested structure. Say, for example, we have two companies and the first one has two branches and the second has just one branch.

Listing 3.1.1 Sample JSON File (items.json)
JSON
{
  "item": {
    "name": "item #1"
    "code": "itm-123"
    "image": {
      "@url": "http://www.foo.com/bar.jpg"
      "title": "bar"
    }
  }
}

Expected JSON output looks as below:

Listing 3.1.2 XML output File (items.xml)
XML
<foo:item xmlns:foo="http://foo.com">
  <foo:name>item #1</foo:name>
  <foo:code>itm-123</foo:code>
  <foo:image url="http://www.test.com/bar.jpg">
    <foo:title>bar</foo:title>
  </foo:image>
</foo:item>

3.2 Install Library

Next, install ChoETL.JSON / ChoETL.JSON.NETStandard nuget package. To do this, run the following command in the Package Manager Console.

.NET Standard Framework

Install-Package ChoETL.JSON

.NET Core

Install-Package ChoETL.JSON.NETStandard

Now add ChoETL namespace to the program.

JavaScript
using ChoETL;

3.3 Quick Conversion

Let's use the library to convert the complex structured JSON to XML with namespaces. It is as simple as can be done with few lines of code. No POCO class needed. It is fast, stream based, and consumes low memory.

Listing 3.3.1. Quick JSON file conversion
JavaScript
private static void QuickConversion()
{
    using (var r = new ChoJSONReader.LoadText("items.json"))
    {
        using (var w = new ChoXmlWriter("items.xml")
            .IgnoreRootName()
            .IgnoreNodeName()
            .WithDefaultXmlNamespace("foo", "http://foo.com")
            )
        {
            w.Write(r);
        }
    }
}

Create an instance of ChoXmlWriter for producing XML (items.xml) output file. Then create an instance of ChoJSONReader object for reading complex JSON (items.json) file.

Where:

  • IgnoreRootName() - tells the writer not to output root element
  • IgnoreNodeName() - tells the writer not to output node element
  • WithDefaultXmlNamespace("foo", "http://foo.com") - tells the writer to use the passed XML namespace for all nodes as default

Sample Fiddle: https://dotnetfiddle.net/MITsuL

3.4 Add Different XML Namespace to Subnode

This sample will show how to add different XML namespace to subnode than root node. Here, you will see how to add "http://temp.com" to image node.

Listing 3.4.1. Add different XML namespace to subnode
JavaScript
private static void AddDifferentNamespaceToSubnode()
{
    using (var r = new ChoJSONReader("items.json"))
    {
        using (var w = new ChoXmlWriter("items.xml")
            .IgnoreRootName()
            .IgnoreNodeName()
            .WithDefaultXmlNamespace("foo", "http://foo.com")
            .WithXmlNamespace("temp", "http://temp.com")
            )
        {
            w.Write(r.Select(rec => 
                             { 
                                 rec.item.image.AddNamespace("temp", "http://temp.com"); 
                                 return rec; 
                             }
                            )
                   );
        }
    }
}

Create an instance of ChoJSONWriter for producing flatten JSON (companies_out.json) output file. Then create an instance of ChoJSONReader object for reading complex JSON (companies.json) file.

Where:

  • IgnoreRootName() - tells the writer not to output root element
  • IgnoreNodeName() - tells the writer not to output node element
  • WithDefaultXmlNamespace("foo", "http://foo.com") - tells the writer to use the passed XML namespace as default to all nodes
  • WithXmlNamespace("temp", "http://temp.com") - tells the writer to use the passed XML namespace as additional namespace to all the subnode and its children.
  • AddNamespace("temp", "http://temp.com") - Use this method to sub node to specify the XML namespace to be used.

The below XML output will be produced after running the code.

Listing 3.4.2. XML output having subnode with different XML namespace
XML
<foo:item xmlns:foo="http://foo.com" xmlns:temp="http://temp.com">
  <foo:name>item #1</foo:name>
  <foo:code>itm-123</foo:code>
  <temp:image url="http://www.test.com/bar.jpg">
    <temp:title>bar</temp:title>
  </temp:image>
</foo:item>

Sample fiddle: https://dotnetfiddle.net/OO0fpx

Download the sample attached above, try it.

For more information about Cinchoo ETL, please visit the other CodeProject articles:

History

  • 27th October, 2021: Initial version

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --