0

Following is my requirement:

Application A is creating a JSON based on its Java Beans and sending to my Application. I have to take this JSON and convert it into XML (XSD for this is completely different than my JSON structure) and send to Application B.

Solution 1) I am currently converting this json to xml using json.org library.Then using Apache-xalan and XSL stylesheet, I am converting this to xml format as required by App B.

Solution 2) Converting this json to Java Bean (JB1).Then converting this JB1 to another Java Bean (JB2) as per the xml structure required by Application B.Then convert JB2 to XML for app B.

Solution 3) Using Apache Xalan and Xerces to parse through the input json and make the XML in Java itself without using XSL.

Which is better approach (in simplicity of code, throughput )? As JSON becomes more complex, is it easy to use solution 1 ? Please suggest if there is better approach other than these 3 ?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Renganathan V
  • 413
  • 1
  • 9
  • 27

4 Answers4

2

XSLT 3.0 offers a built-in json-to-xml() function. Once you have the XML, you can easily transform it to your required format. It is implemented in Saxon 9.7 (PE or higher) and I believe in Exselt.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Same Q and A here http://stackoverflow.com/questions/13007280/how-to-convert-json-to-xml-using-xslt –  Nov 25 '16 at 10:59
1

Solution 1: Yes. This is the conventional and best path for both simple and complex JSON and simple or complex targeted XML.

Solution 2: No. There's no reason to introduce Java Beans as an intermediate form, especially if you have no other need for Java Beans. This option unnecessarily introduces transformational and marshalling complexity.

Solution 3: No. Neither Xalan nor Xerces are designed to parse JSON; they are designed to parse XML.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
0

There are sample programs that will map a JSON document into an equivalent XML document and back; I wrote one as a demo for Liberty's support of json-p (javax.json), using an XML vocabulary I called JinX (JSON in XML). That could be used as a pre/post processor wrapped around XSLT, if desired.

Better solutions are possible -- redefine XSLT to operate on JSON trees, for example -- but would take a bit more work.

keshlam
  • 7,931
  • 2
  • 19
  • 33
-2

JSON is, pure-and-simple, "a communications protocol." In other words, "it specifically exists(!) to allow 'arbitrary (JavaScript) data structures' to be conveyed between some-client and some-host," over "the HTTP(S) protocol."

Therefore: "it is not(!) XML," and therefore must never be considered to be "appropriate input to XSLT!"

"Thou shalt not mix Apples and Oranges!"

If you wish to apply "XSLT" technologies to a "JSON-derived" input (which is, by definition, "a data structure ...") then you must first, and "by whatever suitable means," convert that data structure into XML.

Mike Robinson
  • 8,490
  • 5
  • 28
  • 41
  • In solution1 , I am converting json to xml and then passing this xml to XSLT tehcnology..Is this the same method you are telling ? – Renganathan V Jul 20 '16 at 02:15
  • 1
    The use of "thou shalt" seems quite appropriate in a post that seems to be putting forward an almost religious approach. We're engineers; if we have to join a 3cm plastic outflow pipe to a 6" drain, let's just find the best way to solve the problem, not tell people that thou shalt not do it. – Michael Kay Jul 20 '16 at 09:04
  • My point is simply that: JSON is not XML and therefore it has nothing whatsoever to do with XSLT. *However, that being said,* **XML** is sometimes used as a transport format, and theoretically *that* could be transformed directly using XSLT. – Mike Robinson Jul 20 '16 at 12:00
  • 1
    That is not answer, but I'm not voting down. Web services are being mixed now REST talking to SOAP and back. There is a need since the latest XSLT 3.0 spec includes parse-json() so they see a need. Your views do not translate to answer. –  Nov 25 '16 at 10:59
  • Agree with MK that this is a religious statement. If the JSON folks would get over themselves and accept that there are simple and obvious mappings between JSON and the XML Infoset, they'd immediately be able to leverage all the tooling work that went into XML rather than having to reinvent their own dialects of the same basic solutions. But they were so terrified of not being able to defend their new syntax that they insisted it had to be viewed as Something Different. I think that cost the JSON world several years at least of catch-up development, plus loss of synergy with XML. Sad, really.\ – keshlam Dec 07 '21 at 00:00