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.
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.
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 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)
{
"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)
<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.
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
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
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
<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