DataPower

DataPower

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  Conversion JSON to XML : how to

    Posted Thu March 09, 2023 03:38 PM

    Hi, to all Datapower community

    for all willing to convert JSON to XML and XML to JSON, here are the steps to apply:

    1°/ JSON to XML

    at first, create a MultiProtocolGateway, where Request type is JSON and Response type XML.

    Then in your Processing Policy Rule, ( client to Server, skip-backside = true ), Input : __JSONASJSONX, and only apply the transformation JSON2XML.xslt

    2°/ XML to JSON:

    at first, create a MultiProtocolGateway, where Request type is XMl and Response type JSON.

    Then in your Processing Policy Rule, ( client to Server, skip-backside = true ), only apply the transformation XML2JSON.xslt

    note: attributes of a XML Element will be displayed as follow, example:

    <hello name="mypleasure">world</hello>

    result:

    { "hello" , { "-name": "mypleasure", "#text", "world" } }

    Enjoy The Silence

    Sincerely. Bruno



    ------------------------------
    BRUNO DEMARCHE
    (+33) 6 14120343
    email : brunodemarche@hotmail.com
    expertise in IBM products.
    ------------------------------



  • 2.  RE: Conversion JSON to XML : how to

    Posted Fri March 10, 2023 08:47 AM

    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
    ------------------------------



  • 3.  RE: Conversion JSON to XML : how to

    Posted Fri March 10, 2023 09:52 AM

    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
    ------------------------------



  • 4.  RE: Conversion JSON to XML : how to

    Posted Thu March 30, 2023 09:28 AM

    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
    ------------------------------



  • 5.  RE: Conversion JSON to XML : how to

    Posted Thu March 30, 2023 09:51 AM

    Hi Steve

     

    Many thanks Steve for your feedback. Yes I was aware of this limitation when repeated elements are not consecutive.

    As you may have noticed, the xslt is based on following-sibling to determine whether or not the elements belong to the same array.

     

    As far as I experienced, a pre-requisite for applications is to banned this kind of xml schema, and/or at least develop a xslt that reorganize the incoming flows before conversion.

     

    Hope it helps.

     

    Best Regards

     

    Bruno DEMARCHE