Tip to converting JSON to Xml with namespaces
- Download Cinchoo ETL source
- Download Cinchoo ETL binary (.NET Core)
- Download Cinchoo ETL binary (.NET Framework)
- Working Sample 1 (.NET Fiddle)
- Working Sample 2 (.NET Fiddle)
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)
{
"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 use the passed xml namespace for all nodes as default.
Sample Fiddle: https://dotnetfiddle.net/MITsuL
3.4 Add different xml namespace to subnode
In this sample, will show how to add different Xml namespace to subnode than root node. In here, will show you 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 use the passed xml namespace as default to all nodes.WithXmlNamespace("temp", "http://temp.com")
- tells the writer 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.
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
- 25th October, 2021: Initial version