Hi Bruno,
Your stylesheets look similar to what I wrote for the v5 out of the box policies, which of course are badgerfish aware :-). Just a FYI, one bug I had to resolve for xml to json was one customer had repeating XML elements that were not consecutive which caused issues in creating the JSON array, ie, something like
<parent>
<array>1</array>
<array>2</array>
<string>1</string>
<array>3</array>
<array>4</array>
</parent>
The built-in xml-to-json and json-to-xml policies, especially in v10, are written in native code not xslt and are very fast. Unfortunately for the xml to json they still produce only badgerfish JSON. There is a "relaxed" badgerfish option on the json-to-xml policy that will accept in the JSON either the "key":"value" for standard JSON or the "key": {"$": "value"} badgerfish JSON in producing the XML. Ultimately I'd like to see more formatting type options in these policies, especially for the xml-to-json policy. For example, a plain-JSON choice that would toss XML namespaces and attributes and produce only a standard JSON name:value syntax, and also a relaxed-badgerfish which would only produce badgerfish if the XML element required it (ie, the element had no namespace uris or attributes), but since there hasn't been a lot of demand that work is not planned anytime soon. If that is really a requirement then those customers wanting this should open a request for enhancement (or vote for one that already exists). Enough demand for this could make this requirement show up on the radar. See https://integration-development.ideas.ibm.com/?project=APICONN for submitting RFEs.
Best Regards,
Steve
------------------------------
Steve Linn
Senior Consulting I/T Specialist
IBM
------------------------------
Original Message:
Sent: Fri March 10, 2023 09:51 AM
From: BRUNO DEMARCHE
Subject: Conversion JSON to XML : how to
Hi Steve,
I agree there is no only one way to convert XML flow to a Json format ( due to extension badgerFish, jsonp and so on..).
Also extra characters are supported into one xml format, some others only supported by Json, many representation of array structure in Json, no root element, are the basic issue to deal with.
Here I suggest to write our own method of conversion for a global and non complex structure of flows that suits to many cases in real life.
Please I noticed my Google Drive links for accessing JSONX2XML and XML2JSON were not showned. Let me fix that:
JSONX2XML.xslt
https://drive.google.com/file/d/1x7sSCNsGNSqyFy-2KWNe7QRtgWzI8cmD/view?usp=sharing
XML2JSON.xslt
https://drive.google.com/file/d/1Lojr_DsSJjflEWMaQI2fveVSAZDiIxes/view?usp=sharing
Please let me know.
Best Regards
Bruno DEMARCHE
------------------------------
BRUNO DEMARCHE
------------------------------
Original Message:
Sent: Fri March 10, 2023 08:46 AM
From: Steve Linn
Subject: Conversion JSON to XML : how to
For those more GatewayScript inclined, use the json-xml-converter module, see https://www.ibm.com/docs/en/datapower-gateway/10.5?topic=apis-json-xml-converter-module. The convention it uses for the transformation is badgerfish. A good link to show the badgerfish convention is http://www.sklar.com/badgerfish/. For the XML Bruno has above
<hello name="mypleasure">world</hello>
var converter = require('json-xml-converter');var xmlDoc = '<hello name="mypleasure">world</hello>';var result = converter.toJSON('badgerfish', xmlDoc);
would result in
{ "hello" , { "@name": "mypleasure", "$", "world" } }
If you happen to have a namespace uri in your xml, say
<ns1:hello name="mypleasure" xmlns:ns1="http://example.com/ns1">world</hello>
the namespace will get its own object within the value object where the child properties are the namespace prefix and each property's value would be the namespace-uri
{ "hello" , {"@xmlns": {"ns1": "http://example.com/ns1"}, "@name": "mypleasure", "$", "world" } }
Best Regards,
Steve
------------------------------
Steve Linn
Senior Consulting I/T Specialist
IBM