API Connect

 View Only
  • 1.  XML To JSON Conversion Error

    Posted Fri May 19, 2023 09:26 AM

    HI Team,

    While converting from xml to json policy i am getting below output. i am using can APIC 2018 version . API is also attached. Any idea why this is happening.

    Input :

    <SaveContactResponse>
        <ContactId>1168255</ContactId>
        <HasErrors>false</HasErrors>
    </SaveContactResponse>

    Output:

    {"SaveContactResponse": {
       "ContactId": {"$": "1168255"},
       "HasErrors": {"$": "false"}
    }}

    BR

    Fahad



    ------------------------------
    Fahad Hassan
    ------------------------------


  • 2.  RE: XML To JSON Conversion Error

    Posted Mon May 22, 2023 09:36 AM

    Hi Fahad,

    Just as a reminder the 2018 release is no longer in support and you should be planning on upgrading, but the symptom you show is working by design and you'd experience it in the latest release.  The XML2JSON policy uses a convention known as badgerfish.  See http://www.sklar.com/badgerfish/ for details.  This convention will preserve  XML attribute and namespace information and always specifies the value in a child "$" property.  As an example, if you XML had 

    <ns1:SaveContactResponse xmlns:ns1="http://example.com/ns1" attribute="hello">
        <ContactId>1168255</ContactId>
        <HasErrors>false</HasErrors>
    </SaveContactResponse>

    the JSON that would be produced would have

    {"ns1:SaveContactResponse": {
       "@xmlns" : {"ns1": "http://example.com/ns1"},
       "@attribute": "hello",
       "ContactId": {"$": "1168255"},
       "HasErrors": {"$": "false"}
      }
    }

    Still, there have been a number of customers that have opened a request for enhancement to specify an option to this policy to either produce a "plain" JSON document that would ignore any attributes and namespace URIs and produce a simple name:value pair, or perhaps a "relaxed-badgerfish" format that would only use the badgerfish syntax it the XML element required it due to the presence of an attribute or namespace URI.  In looking at the RFE site https://integration-development.ideas.ibm.com/?project=APICONN I'm only seeing one related RFE for formats other than badgerfish, so I think you'll need to open a new RFE for this capability.  In the interim, many customers have written a "de-badgerfish" GatewayScript policy that simply recursively steps through the badgerfish JSON object and if there is a $ property, simply tosses the object and sets the value of the parent property to the value of the $ property, otherwise if an array or object type, it recursively calls itself passing in the child object.  I've also seen some customers do this with a map policy where the input and output schemas are the same, they simply map the input object to the output object and enable the "JSON post processing" option that will compare the schema to the payload and will "de-badgerfish" the result as part of the post processing.  The added benefit will be to get around some of the inherent issues with the XML to JSON policy given it is doing this transformation without any knowledge of the JSON schema.  For example

    <parent>
      <array>only element of  an array</array>
      <boolean>false</boolean>
      <string>12345</string>
      <number>12345</number>
    </parent>

    if there were two <array> elements, you'd have in the JSON "array":["first element", "second element" ...] but in the above case with only one element, you'd get a string or an object depending upon the value of the element, not an array, ie,  "array": "only element of an array" As for the boolean and number elements, is false or 12345 a string or number?  In the XML there is no datatype indication, and the conversion will assume a string.  The map policy will use the schema and will make these values to be consistent with the schema.

    Best Regards.

    Steve



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



  • 3.  RE: XML To JSON Conversion Error

    Posted Fri May 26, 2023 02:57 AM

    HI @Steve Linn ,

    Thank you for explaining and providing solution.

    BR

    Fahad



    ------------------------------
    Fahad Hassan
    ------------------------------