Issue with XMLToJSON Policy

We are facing one problem when we use XMLtoJSON policy,

if XML structure is like this:

<a>

<b>text1</b>

<b>text2</b>

</a>

then its rightly converting it into an array

"b":["text1","text2"]

but when we have single element

<a>

<b>text1</b>

</a>

then it won't convert it into array, it will look like this:

"b":"text1"

Our requirement is that in case of single element, it has to be converted into an single element array something like this:

"b":["text1"]

As of now, we are trying to correct it through java script, but if there is more elegant way to do this, please let us know

1 4 253
4 REPLIES 4

@Sunandita Dam ,

Single element arrays : They look identical to a single object, so the JSON will show an object instead of an item in an array. Javascript is the way to implement for now if you would like to convert same to array.

Ideally, there should be an option in the policy which decides single element arrays should be treated as arrays or object. Again, someone will say i would like to selectively apply the option to specific elements in XML. Javascript is the rescue to handle these edge cases.

Just FYI, there is an outstanding ticket, APIRT-1144, which extends the XMLToJSON policy to allow this automatically, without resorting to a JS callout.

For today, JS is the answer. It should be easier, in the future. (soon)

Hi @Sunandita Dam,

It is the default behavior of XMLtoJSON policy, that is it converts an element with single occurence into (object/string) and coverts it into an array only if the element has multiple occurence.

1. We have faced similar issue and resolved it using XLST. Since we were dealing with complex SOAP payload (response from provider) we had used XSLT transform policy to create our simple response structure and then used XMLtoJSON policy to covert the xml to json.

In the XSLT, the elements that are required to be an array irrespective of number of occurences in the SOAP response, we added an empty tag for that element. Addition of empty tag always creates two instances of the element and hence ensure that XMLtoJSON policy always converts it into an array.

2. Another solution can be creating the JSON response in XSLT itself. This allows you to create the JSON structure as per requirement.

For example:

"items" : [

                        
    <xsl:for-each select="./Envelope/Body/response/ItemList">

     {
      "itemName" : "<xsl:value-of select="./itemName" />",

      "itemDescription": "<xsl:value-of select="./itemDescription"/>"

     }<xsl:if test="following-sibling::ItemList">,</xsl:if>

    </xsl:for-each>

  ]

3. If use of XSLT is out of scope then javascript is the only solution.

You can go through this article for more details.

Please find similar post here.

We fixed the problem using JS code. Below is the approach that was followed.

var str = JSON.stringify(obj);
var tokens =
obj.getTokensbyGUIDResponse.Response.getTokensbyGUIDResp.TokensbyGUIDList.TokenNbr;

arrLen =
obj.getTokensbyGUIDResponse.Response.getTokensbyGUIDResp.TokensbyGUIDList.TokenNbr.length;
if(!arrLen > 0){   

var resp = str.replace(tokens,"["+tokens+"]");

}