Converting XML data to JSON data can be difficult, a case in point is where the XML Schema (XSD) specifies that an element is a collection (i.e. maxOccurs >1) of type xs:any as shown in the image below:
If the collection was a concrete type, say a list of author elements, this scenario would simply produce an array in the converted JSON output:
If we have XML data such as:
you could say that if there is more than one 'row' value present, then we should write an array, but this leads to output that is quite different depending on the input.
i.e. if there is only a single item in the XML, then the JSON output produced would be something like:
but if there were multiple items in the XML, then the JSON output produced would be:
It also means the writer would need to hold the state of the last processed object in order to know whether to write an object or an array, and this does not work well with stream based writers.
Whilst not ideal, the output is consistent and as the JSON specification places no restrictions on duplicate keys this is a valid approach.
If possible, a better solution would be to change the XML Schema (XSD) to specify a concrete type for the data rather than using xs:any, so the JSON writer would then know that the 'row' element is a collection and will write it as an array.
What is Liquid XML Objects?
Liquid XML Objects is a direct replacement for Microsoft Visual Studio's xsd.exe and includes better XSD 1.0 standard support, support for W3C XSD 1.1, support for JSON serialization and much more. The XML Objects generator is integrated into Microsoft Visual Studio and support C# and Visual Basic .Net.
What is Liquid XML Data Binder?
Liquid XML Data Binder creates simple, intuitive code from your W3C XML Schema (XSD). The generated code library contains strongly typed classes, collections, and enumerations to create an intuitive custom API to code against from your C++, Java or VB6 (COM) source code.
Download a Free Trial and Free Community Version of Liquid XML Objects and Liquid XML Data Binder: