API Connect

 View Only
  • 1.  Best practices for XML to JSON and vice versa

    Posted Thu August 27, 2020 11:44 AM
    Hi Guys

    I am leading an API connect implementation and often come across various ways using with our developers do transformation in API Connect. Most of the developers prefer the map policy since it has the GUI and it works well until we get some into complex transformations where merging or transforming arrays is required.

    We typically move to gateway script in case of complex json to json transformations and XSLs for XML to XML, however when it comes to complex JSON to XML and vice versa there is no real standard that we follow.

    I would say that JSON to XML is still a little easy but just putting a JSON-TO-XML policy infront of a XSL but XML to JSON becomes dirty while coding.

    Is there a standard practice that you guys would recommend?

    ------------------------------
    Vaibhav Mehra
    ------------------------------


  • 2.  RE: Best practices for XML to JSON and vice versa

    Posted Tue September 01, 2020 09:45 AM
    Literally transforming an XML message to the JSON equivalent may result in a not-ideal JSON structure indeed. Would using a map policy, with a definition of the JSON object you want to map to, be an option?

    ------------------------------
    Johan Thole
    IT Specialist
    IBM
    De Meern
    ------------------------------



  • 3.  RE: Best practices for XML to JSON and vice versa

    Posted Wed September 02, 2020 03:01 AM
    Hi Johan

    Map policy does the job for straight forward mappings but when we need to merge and filter arrays left to right, it is incapable of handling it. In such scenarios we end using multiple policies sometimes XML to JSON policy followed by a map or gateway script and sometimes an XSL to an intermediate XML and then a Map. So there is no consistent way. 

    I am interested in knowing how others are handling such scenarios?


    Regards
    Vaibhav

    ------------------------------
    Vaibhav Mehra
    ------------------------------



  • 4.  RE: Best practices for XML to JSON and vice versa

    Posted Wed September 02, 2020 09:03 AM
      |   view attached

    Hi Vaibhav,

    The XML to JSON policy outputs the JSON using the Badgerfish convention.  This was done as the policy is general purpose and has to handle XML with namespaces and attributes.  The Badgerfish artifacts would not be needed if the XML document did not utilize namespaces and attributes.  I would suggest you submit a request for enhancement using https://www.ibm.com/developerworks/rfe/?PROD_ID=1517 to request an option on this policy to output "plain" JSON, ie, throwing away any namespace or attributes in the XML to produce a JSON with name:value pairs at leaf properties, or even a "relaxed" JSON, ie, only producing the Badgerfish artifacts when those input XML elements have namespaces and attributes, otherwise producing the "plain" JSON for those elements.

    In the meantime, customers can use the XML to JSON policy and then follow it with a GatewayScript policy that "de-badgerfishes" the JSON.  The example GatewayScript code I have attached will throw away any namespace or attributes nodes from the XML in the resulting JSON, producing the "plain" JSON that you're wanting.

    As for the map policy and "complex transformations where merging or transforming arrays is required",  I'll note that many issues seen in this space were caused by the map policy UI not effectively creating the map actions in the swagger for these use cases.  Significant enhancements to the UI for array mappings were made in the 5.0.8.8 time frame (fall of 2019), but you also have the option in some cases to simply map from one input object/array to the output array directly instead of using the individual properties, and then write the complex mapping GatewayScript by clicking on the green circle at the output property to bring up the Configure Map dialog.  This approach would allow you to map the easier parts of the schema as always but add special handling for these complex arrays, all in one map policy.  I'm working on a developerWorks article that will provide some examples of these use cases that will hopefully be published in the near future.

    Best Regards,

    Steve


    Attachment(s)

    js
    removeBadgerFish.js   1 KB 1 version


  • 5.  RE: Best practices for XML to JSON and vice versa

    Posted Fri September 11, 2020 11:31 AM
    Thanks Stephen

    We do use the de-badgerfish script often in case of XML to JSON transformations. Will try the upgrade to 5.0.8.8 as the improvements in the MAP policy certainly will impact developer productivity.

    Regards
    Vaibhav

    ------------------------------
    Vaibhav Mehra
    ------------------------------