XMLtoJSON policy - how to transform the following case correctly?

My original XML looks like this:

<v14:CodigoDelCupoControlado xsi:nil="true" 
    xmlns:v14="http://esb.cla.cl/EnterpriseObjects/Credito/V1" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

The output of my XMLtoJSON is like this:

"CodigoDelCupoControlado": {
    "nil": true,
    "": ""
}

Desired output:

"CodigoDelCupoControlado": ""

By modifying the XML to remove the xsi:nil attribute, before applying the XMLToJSON policy, I can get the desired result. The JavaScript for this "Temporary solution":

EdwinFerreira_0-1661792583222.png

Is there a better solution that does not involve adding a JS policy to remove all nil?

 
Solved Solved
0 1 397
1 ACCEPTED SOLUTION

The problem is, as you have found, the attribute on the CodigoDelCupoControlado element. The XMLToJSON policy will try to do the right thing. 

  • when it sees an XML element with simple text content, it will convert that to a simple JSON string
  • when it sees an XML element with text plus attributes, it will convert that to a JSON hash, using different fields in the hash to store the attribute values and the text values.

There is no way to tell the XMLToJSON policy to simply "ignore" the attributes on every element. That seems like a possibly interesting feature, but I've never heard of anyone asking for that. 

So the "temporary solution" you've developed, is a good way to handle this situation. 

If you don't like that, there are some alternatives I can think of: 

  • you could also use an XSL to transform the XML to remove xsi:nil, as a precursor step. 
  • Use a single JavaScript step alone, to transform from XML to JSON. You would need to rely on the e4x extensions to JavaScript to do this. (search here on Apigee community for examples)
  • You could build your own Java callout to do the transform while ignoring xsi:nil

None of these are particularly "better" than what you have already found.

 

 

View solution in original post

1 REPLY 1

The problem is, as you have found, the attribute on the CodigoDelCupoControlado element. The XMLToJSON policy will try to do the right thing. 

  • when it sees an XML element with simple text content, it will convert that to a simple JSON string
  • when it sees an XML element with text plus attributes, it will convert that to a JSON hash, using different fields in the hash to store the attribute values and the text values.

There is no way to tell the XMLToJSON policy to simply "ignore" the attributes on every element. That seems like a possibly interesting feature, but I've never heard of anyone asking for that. 

So the "temporary solution" you've developed, is a good way to handle this situation. 

If you don't like that, there are some alternatives I can think of: 

  • you could also use an XSL to transform the XML to remove xsi:nil, as a precursor step. 
  • Use a single JavaScript step alone, to transform from XML to JSON. You would need to rely on the e4x extensions to JavaScript to do this. (search here on Apigee community for examples)
  • You could build your own Java callout to do the transform while ignoring xsi:nil

None of these are particularly "better" than what you have already found.