Page 1 of 1

roundtrip XML-JSON and back

Posted: Fri Sep 09, 2022 7:13 am
by bobstuart
When I try to roundtrip XML to JSON and back using the GUI tools in Oxygen 24.1 I have problems with namespaces. Should I be able to roundtrip?
Trivial example
<?xml version="1.0" encoding="utf-8"?>
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<!-- XML Catalog file for DocBook XSL-NS v1.78.1 -->
<rewriteURI uriStartString="http://docbook.sourceforge.net/release/xsl-ns/current/" rewritePrefix="./"/>
<rewriteSystem systemIdStartString="http://docbook.sourceforge.net/release/xsl-ns/current/" rewritePrefix="./"/>
<rewriteURI uriStartString="http://docbook.sourceforge.net/release/xsl-ns/1.78.1/" rewritePrefix="./"/>
<rewriteSystem systemIdStartString="http://docbook.sourceforge.net/release/xsl-ns/1.78.1/" rewritePrefix="./"/>
</catalog>

becomes
{
"catalog": {
"xmlns": "urn:oasis:names:tc:entity:xmlns:xml:catalog",
"rewriteURI": [
{
"uriStartString": "http://docbook.sourceforge.net/release/xsl-ns/current/",
"rewritePrefix": "./"
},
{
"uriStartString": "http://docbook.sourceforge.net/release/xsl-ns/1.78.1/",
"rewritePrefix": "./"
}
],
"rewriteSystem": [
{
"systemIdStartString": "http://docbook.sourceforge.net/release/xsl-ns/current/",
"rewritePrefix": "./"
},
{
"systemIdStartString": "http://docbook.sourceforge.net/release/xsl-ns/1.78.1/",
"rewritePrefix": "./"
}
]
}
}
and then
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<xmlns>urn:oasis:names:tc:entity:xmlns:xml:catalog</xmlns>
<rewriteURI>
<uriStartString>http://docbook.sourceforge.net/release/ ... tartString>
<rewritePrefix>./</rewritePrefix>
</rewriteURI>
<rewriteURI>
<uriStartString>http://docbook.sourceforge.net/release/ ... tartString>
<rewritePrefix>./</rewritePrefix>
</rewriteURI>
<rewriteSystem>
<systemIdStartString>http://docbook.sourceforge.net/release/ ... tartString>
<rewritePrefix>./</rewritePrefix>
</rewriteSystem>
<rewriteSystem>
<systemIdStartString>http://docbook.sourceforge.net/release/ ... tartString>
<rewritePrefix>./</rewritePrefix>
</rewriteSystem>
</catalog>

Re: roundtrip XML-JSON and back

Posted: Fri Sep 09, 2022 8:47 am
by florin_nica
Hi Bob,

Thank you for the detailed feedback.

The XML to JSON conversion tool does not differentiate between XML attributes (including namespaces) and XML elements, so both of them are converted to JSON properties. When converting back to XML, all the JSON properties become XML elements, so the roundtrip is not a perfect match.

We already have an internal issue (EXM-48884), which requires some investigation in this regard. A possible solution could be to somehow annotate or prefix the JSON properties that are converted from XML attributes, so that the conversion tool will know how to make the exact roundtrip.

I will keep you up-to-date with the status of the issue.

Regards,
Florin

Re: roundtrip XML-JSON and back

Posted: Fri Sep 09, 2022 5:31 pm
by bobstuart
Good to know it's not just me somehow. An old post her pointed me to https://github.com/bramstein/xsltjson/ maybe that's the underpinning of your XML to JSON? He has info on a few conventions that might make it round trip able. It appeared BadgerFish would do so but I didn't quickly find a BadgerFish JSON to XML lots of XML to BadgerFish JSON but not the return trip.

Just a thought; I don't know if BadgerFish is old and not in use or still going strong.

Re: roundtrip XML-JSON and back

Posted: Mon Sep 12, 2022 2:00 pm
by florin_nica
Hi again Bob,

Thank you very much for the suggestion.

The Badgerfish convention seems to be a very comprehensive way of converting XML to JSON, as it uses some mappings for XML attributes and namespaces. This adds a bit of overhead to the JSON syntax, but, of course, there has to be a trade-off.

Maybe we can use these mappings as well in our XML-JSON converter. But not by default, rather controlled by some options.

Regards,
Florin