1
How to convert file with JSON collection to file with XML collection (XSD has only 1 element)?
Question asked by Michael Lavatman - 6/26/2024 at 10:09 PM
Answered
Hello,
I need to convert a file with collection of JSON objects under a common root to a file with collection of XML nodes under a common root.
I have .xsd file that defines XML schema for single node of destination.
If I use a file with single JSON object I can easily map it to XML, but how do I map a file with JSON collection to XML collection? Is this possible at all? I don't know in advance how many elements are in my source collection, so I can't possibly create XSD schema with the same number of elements.
I want to avoid dealing with single files for each element and then merging them together as one XML file.
Thank you,
Michael

2 Replies

Reply to Thread
0
Michael Lavatman Replied
I think I understand it now.
When I use the mapper I need to manually replicate object on the right side to match the number of objects on the left side and link individual fields.
If I convert it to C# code I have to iterate through the array and programmatically create array/collection of object dynamically.
There is no magical solution here.
Please confirm my thoughts.
Thanks,
Michael
0
Liquid Support Replied
Employee Post Marked As Answer
The following example creates an XML Address element for every item in the source JSON files array.
The  Item[0-n] connection point is basically each element in the array. When the array item is a JSON object, then there will be a valid entry for Value (Object) - it acts like a filter on the array item type.
So for each item in the JSON array a node is created on Item [0-n], by connecting this to the XML Address connection point you are saying that for each item in the JSON array, create an XML Address object.

So if the source files looks like this

[
{
"CountryCode": "BE",
"CapitalName": "Brussels"
},
{
"CountryCode": "CA",
"CapitalName": "Ottawa"
},
{
"CountryCode": "CN",
"CapitalName": "Beijing"
},
...
Then the resulting XML will look like this
<Bookstore xmlns="http://www.liquid-technologies.com/sample/bookstore">;
<Address>
<County>Brussels</County>
</Address>
<Address>
<County>Ottawa</County>
</Address>
<Address>
<County>Beijing</County>
</Address>
....
As you say if you want to copy the structure of the JSON to XML you will have to connect up all the child nodes as required, by using the technique shown above you can create collections etc, so if you have 10 instances of the same object, you only need to define this once in the transform, but if you have 10 different types of object you will need to wire up all of them individually.

If I have misunderstood your issue, please provide a sample JSON and expected XML (Ideally with the schemas) and I will investigate further.

Reply to Thread