API Connect

 View Only
Expand all | Collapse all

Conversion from json to xml

  • 1.  Conversion from json to xml

    Posted Thu December 30, 2021 09:51 AM
    Hi,

    How to use json-to-xml transform policy in ApicV10 to convert the content of our request body payload(Or any sample data in json format) from json to xml. Please provide us if any reference available for this conversion.

    ------------------------------
    Thanks and regards,
    Suraj
    ------------------------------


  • 2.  RE: Conversion from json to xml

    Posted Mon January 03, 2022 04:11 PM
    Hi Suraj! You can check out IBM documentation for reference: https://www.ibm.com/docs/en/api-connect/10.0.x?topic=policies-json-xml

    If you want to see an example, you can see my repository

    ------------------------------
    Maria Cecilia Bel
    ------------------------------



  • 3.  RE: Conversion from json to xml

    Posted Tue January 04, 2022 10:46 AM
    Hi Maria,

    Thanks for reply.
    We are trying to import provided json-to-xml_1.0.0.yaml file into our apic v10 system but when we click on online button then it shows following error
    "Failed to make API online
    The product 'jsontoxml-product' cannot be staged to the 'sandbox' catalog because the product requires a DataPower API Gateway, and no gateway service of this type is configured in the catalog settings."


    ------------------------------
    Suraj Sakpal
    ------------------------------



  • 4.  RE: Conversion from json to xml

    Posted Tue January 04, 2022 10:58 AM
    It's a gateway compatibility error. I was using a DP API gateway when I created the API. Are you using a version 5 compatible gateway? If so, the policies are different. 
    But basically, I did what Steve is saying. You need to parse the payload first, before using the json to xml policy.

    ------------------------------
    Maria Cecilia Bel
    ------------------------------



  • 5.  RE: Conversion from json to xml

    Posted Tue January 04, 2022 11:10 AM
    Yes we are using V5 datapower compatible gateway (datapower-gateway). You mean to add parse policy before json to XML policy in assembly section or any other way?

    ------------------------------
    Suraj Sakpal
    ------------------------------



  • 6.  RE: Conversion from json to xml

    Posted Tue January 04, 2022 01:03 PM
    You need to parse if you are using API Gateway (like the one in my repo).
    The parse policy is not required (or even available) in the v5 compatible gateway. You can check this link for instructions on how to configure the json to XML policy in the assembly interface.

    ------------------------------
    Maria Cecilia Bel
    ------------------------------



  • 7.  RE: Conversion from json to xml

    Posted Tue January 04, 2022 03:30 PM

    Hi Suraj,

    Review your source of your API.  Specifically

    • the gateway type  - v10 api-gateway would have a value of datapower-api-gateway versus v5/v5c would have a value of datapower-gateway.

    • the json-to-xml policy version - v10 should be version 2.0.0 whereas v5/v5c should be version 1.0.x


    Your error is indicating that either your API is indicating it is a API Gateway API but your sandbox catalog does not have an API Gateway associated with it.  To get your policy to work in v5c, simply change the api type and policy version of every policy to be a 1.0.x policy.  Note that v5c does not have a parse policy, that is for the API Gateway only.

    Regards,
    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 8.  RE: Conversion from json to xml

    Posted Wed January 05, 2022 07:49 AM
    Hi Steve,

    Thanks for reply
    Yes you are right. But when we change api type and other policy version to 1.0.0 it shows following error:
    "Failed to make API online
    The product jsontoxml-product:1.0.0 (jsontoxml product) references the following APIs that have an x-ibm-configuration.gateway value of 'datapower-gateway' which is incompatible with the product's gateway value of 'datapower-api-gateway': json-to-xml:1.0.0 (JSON to XML)"
    And without parse policy in v5c does this api work?

    ------------------------------
    Suraj Sakpal
    ------------------------------



  • 9.  RE: Conversion from json to xml

    Posted Wed January 05, 2022 10:05 AM

    Hi Suraj,

    From your description it sounds as if changing the gateway type on a published API is having some difficulty.  Can you try to retire and delete the existing product from your catalog and then add the API to a new catalog?  As for the parse policy, that is an API Gateway only and is unknown to a v5 compatible gateway.

    Regards,

    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 10.  RE: Conversion from json to xml

    Posted Tue January 04, 2022 09:00 AM

    Hi Suraj,

    The json-to-xml policy is very straight forward.  One thing to do in APIC v10 is to parse the inbound JSON payload using a parse policy.  The json-to-xml policy needs a parsed JSON document as input.  The other suggestion is to set your conversion format to "relaxed badgerfish" which should take plain json such as

    {"hello":"world"}

    and convert it to 

    <hello>world</hello>

    whereas the default type of badgerfish would fail as it would assume your input was of the badgerfish format

    {"hello": {"$": "world"}}


    Regards,
    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 11.  RE: Conversion from json to xml

    Posted Fri March 04, 2022 04:02 AM
    Hi Steve,
    Thank you for your suggestion on this matter. We are also seeing a similar issue while converting XML to JSON. by default its converting to Badgerfish format and there are no other options except v5 version format.  I would like to know how do we select "relaxed badge fish" as you mentioned. 

    We are using v10 DataPower gateways.

    ------------------------------
    Arjun Pilli
    API Specialist
    DSV
    Johannesburg
    +27 0840611655
    ------------------------------



  • 12.  RE: Conversion from json to xml

    Posted Mon March 07, 2022 05:09 PM
      |   view attached

    Hi Arjun,

    Unfortunately, the XML2JSON policy in v10 does not have a "relaxed badgerfish" format option.  The requirement is not new for sure, but I would suggest you open a request for enhancement at the Aha site https://integration-development.ideas.ibm.com/?project=APICONN.  The more this need is socialized, the more likely it may eventually get some attention.  In the meantime, the only option I have to provide is to run a "de-badgerfish" GatewayScript policy after the XML2JSON.  I've provided that source in the forum before, but I'll post here as well. 

    Regards,

    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------

    Attachment(s)

    js
    removeBadgerFish.js   1 KB 1 version


  • 13.  RE: Conversion from json to xml

    Posted Tue March 08, 2022 03:38 AM
    Thank you, Steve. Will do. I will also request our IBM account manager to push this RFE.

    ------------------------------
    Arjun Pilli
    API Specialist
    DSV
    Johannesburg
    +27 0840611655
    ------------------------------



  • 14.  RE: Conversion from json to xml

    Posted Thu April 28, 2022 08:56 AM
    Dear Arjun,

    You can achieve your expected json format post xml-to-json policy, all you can place below gws code after convert policy

    https://www.ibm.com/support/pages/how-convert-xml-json-format-other-badgerfish-using-datapower-gateway

    var util = require('util'); 
     var bfJson = apim.getvariable('message.body'); 
     //console.alert(JSON.stringify(bfJson)); 
     var plainJson = {}; 
     convertBadgerfishToPlainJSON(bfJson, plainJson ); 
     //console.alert(JSON.stringify(plainJson)); 
     apim.setvariable('message.body', plainJson); 
     apim.output('application/json'); 
      
     function convertBadgerfishToPlainJSON(inputDocument,outputDocument) { 
       for (var property in inputDocument) { 
         // namespace and attribute properties start with an @, so ignore them 
         if (property.slice(0, 1) != '@') { 
           // get this property's value. 
           var propertyObj = inputDocument[property]; 
           // for namespaces, badgerfish includes the namespace property name, so strip it out 
           var i = property.indexOf(':'); 
           if (i >= 0) { 
             property = property.slice(i + 1); 
           } 
      
           // process the property's object based on its type 
           var type = util.safeTypeOf(propertyObj); 
           switch (type) { 
           case 'object': 
             // objects, a child of $ is the property's value 
             if(propertyObj['$']) { 
               outputDocument[property] = propertyObj['$']; 
             } else { 
               // an object without a child $, this property has child objects, so recurse to convert the children 
               outputDocument[property] = {}; 
               convertBadgerfishToPlainJSON(propertyObj,outputDocument[property]) 
             } 
             break; 
           case 'array': 
             // any array, recurse to convert the array children 
             outputDocument[property] = []; 
             convertBadgerfishToPlainJSON(propertyObj,outputDocument[property]) 
             break; 
           default: 
             // don't think we should get here, but just in case, set the property to its scalar value 
             outputDocument[property] = propertyObj; 
             break; 
           } 
         } 
       } 
     }



    ------------------------------
    Jayprakash Yadav
    ------------------------------