Re: [xsl] Convert JSON to XML?

Subject: Re: [xsl] Convert JSON to XML?
From: "Michael B Allen ioplex@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 2 May 2025 12:07:22 -0000
On Thu, May 1, 2025 at 6:35b/PM dvint@xxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> I know (and have been using) the xlst json-to-xml() function for this
> purpose, but it is a pretty basic result. I happened to try the JSON to
> XML funtion in oXygen and got what I'd say is a much better result where
> I had some unique elemeent names instead of everything being a map or a
> string element.
>
> I was poking around to try and find a tool/library I could use in my
> conversion path and everything I'm finding is an online tool or has a
> price. Currently I'm developing a conversion pipeline and can leverage
> oXygen for this, but I need something I can build into a tool and load
> into a Docker image for the production implementation.
>
> Anyone have a suggestion?
>

Hi Dan,

It is not possible to map JSON to XML in a "correct" way because a JSON
element can only be either a map or a list and not both whereas each XML
element is both a map and a list.
As a result there are many ways to represent the same data.

And frequently what is perceived as "correct" is really not technically or
conceptually.
For example, it is commonplace to do something like the following:

<address>
  <street>123 Main St</street>
  <city>Townville</city>
  <state>PA</state>
</address>

IMO the "correct" form for an XML element is to put values that are not
divisible into attributes which means the truely correct way to represent
this data is:

<address
  street="123 Main St"
  city="Townville"
  state="PA"/>

Anyway, my point is that the only "correct" way to convert JSON to XML is
to understand in advance which of the many XML representations is "correct"
for your particular application and then write code to explicitly perform
that conversion.

It's also not something that necessitates a third party tool as it is
almost trivial to write code to recur through the JSON structure and emit
XML text directly.

Mike

--
Michael B Allen
Java AD DS Integration
https://www.ioplex.com/ <http://www.ioplex.com/>

Current Thread