DataPower

 View Only
  • 1.  xml to json transformation

    Posted Tue May 07, 2024 05:17 AM

    Hi all, 

    Is there any generic way to transform xml to json? 

    Or I will need to follow xml --> jsonx transformation using custom xslt --> json (using inbuilt jsonx2json xsl)? 

    Requirement : 
    Input (xml) --> DP --> Output(json)

    Thanks ! 
    - Prathamesh 



    ------------------------------
    Prathamesh Dixit
    ------------------------------


  • 2.  RE: xml to json transformation

    IBM Champion
    Posted Tue May 07, 2024 09:48 AM

    We'll need to know your definition of "generic".  I think one can argue that the in-box xml -> jsonx transform is the DataPower generic way.

    However, I'm assuming you want something in particular, and the scope of any-XML to any-JSON can become quite large considering all the possibilities.

    But, what I would do is to use DataPower's xml -> jsonx, then use a GatewayScript to process through the resulting JSON to turn it into what you want.  A way to do that, generically, is using "Object.keys(theJSON)" and then iterating through the keys and getting the values of that key.    Something like this:

    // process the incoming buffer. then....
    
    var jsonKeys = Object.keys(theJSON);
    
    for (var i = 0; i < jsonKeys.length; i++) {
         var keyName = jsonKeys[i];
         var value = theJSON[keyName];
    
        // Do what you want here
    }
    

    Depending up on nesting and other value types you may encounter, you'll likely want the above code snippet inside a recursive function of some kind.

    Honestly, can't help much more at this point, but I hope this gets you started.



    ------------------------------
    Joseph Morgan
    ------------------------------



  • 3.  RE: xml to json transformation

    Posted Wed May 08, 2024 02:31 AM

    Hi Joseph, 

    Thanks for your insight again ! 

    Pardon my knowledge here - what do you mean by "use DataPower's xml -> jsonx" ? Do we have any inbuilt functionality to do so without creating any custom xslt? 

    Thanks ! 
    Prathamesh



    ------------------------------
    Prathamesh Dixit
    ------------------------------



  • 4.  RE: xml to json transformation

    IBM Champion
    Posted Wed May 08, 2024 11:22 AM

    I was referring to the converter module Hermann was referring to and mistakenly typed "jsonx" instead of "json".

    It isn't really that difficult.  Think of GatewayScript as Node.js with hooks into DataPower.  



    ------------------------------
    Joseph Morgan
    ------------------------------



  • 5.  RE: xml to json transformation

    Posted Tue May 07, 2024 11:38 AM

    Prathamesh,

    first, there are infinitely many different mappings from XML to JSON.

    Then DataPower has json-xml-converter module ...
    https://www.ibm.com/docs/en/datapower-gateway/10.5.x?topic=apis-json-xml-converter-module

    ... allowing you to convert in both directions.
    The JSON format for that module is called "BadgerFish" and linked to at bottom of the module.



    ------------------------------
    Hermann Stamm-Wilbrandt
    Compiler Level 3 support, IBM DataPower Gateways
    IBM
    Boeblingen Germany
    ------------------------------



  • 6.  RE: xml to json transformation

    Posted Wed May 08, 2024 02:36 AM

    Hi Hermann, 

    Thanks for your input. I lack expertise in gatewayscript & Badgerfish json. I think I need to explore it in depth to follow your recommendation. I will look into this too. 

    Meanwhile, Is there any alternative to just convert xml--> jsonx? 

    Thanks !
    Prathamesh



    ------------------------------
    Prathamesh Dixit
    ------------------------------



  • 7.  RE: xml to json transformation

    Posted Thu May 09, 2024 01:35 PM
    Edited by DOMINIC MICALE Thu May 09, 2024 01:52 PM

    We have a good technote about this all here to use gatewayscript to create non badgerfish json (in the answer) https://www.ibm.com/support/pages/how-convert-xml-json-format-other-badgerfish-using-datapower-gateway

    Here is a full example with using badgerfish in gatewayscript: https://gist.github.com/copethomas/9891c9f3f1b226e1e738b4926813974a

    DataPower natively only supports a JSON to JSONx (XML) converter you can see information about that here https://www.ibm.com/docs/en/datapower-gateway/10.5.0?topic=jsonx-json-as-in-conversion-action

    You would need the API Connect product (and API Gateway) to have an "action" for XML to JSON https://www.ibm.com/docs/en/api-connect/10.0.x?topic=policies-xml-json



    ------------------------------
    DOMINIC MICALE
    ------------------------------



  • 8.  RE: xml to json transformation

    Posted Wed May 15, 2024 03:27 AM

    Hi Dominic , 

    Thanks for your suggestions. Let me go through it and connect back in case of any more inputs required. 
    Much appreciated. 

    Cheers ! 
    Prathamesh



    ------------------------------
    Prathamesh Dixit
    ------------------------------