JSON Array to XML Array conversion Issue in response object

mohanr82
Participant I

My back end API supports only JSON. So in order satisfy XML customer, We did XML to JSON conversion in APIGEE layer.We are able to convert XML array in to JSON array and pass request to back end API. Same way we are expecting JSON array response from back end API,but XML conversion returning only single value instead of array. Let me know how to fix this issue

0 8 567
8 REPLIES 8

Could you share your snippet of code between

<JSONToXML> </JSONToXML>

And the JSON payload. Feel free to mask out sensitive details.

mohanr82
Participant I

<?xml version="1.0"?> -<JSONToXML name="deliverySchedule-JX-JSONToXmlConversion"> <Properties/> -<Options> <NullValue>NULL</NullValue> <NamespaceBlockName>#namespaces</NamespaceBlockName> <DefaultNamespaceNodeName>$default</DefaultNamespaceNodeName> <NamespaceSeparator>:</NamespaceSeparator> <TextNodeName>#text</TextNodeName> <AttributePrefix>{replace9}lt;/AttributePrefix> <InvalidCharsReplacement>_</InvalidCharsReplacement> <ObjectRootElementName>VesselSchedule</ObjectRootElementName> <ArrayRootElementName>VesselDetails</ArrayRootElementName> <ArrayItemElementName>Item</ArrayItemElementName> </Options> <OutputVariable>response</OutputVariable> <Source>response</Source> </JSONToXML>

above xml used for the JSON to XML conversion in Response

The policy looks fine. What's the content of JSON payload ?

{"VesselScheduleResponse": { "Header": { "SenderID": "1234", "ReceiverID": "ABCD", "TransactionDate": "2018-12-20", "TransactionTime": "14:00:00" }, "VesselDetails": [ { "TransactionReferenceId": "201812203001", "VesselName": "122", "VoyageNumber": "DCTEST31", "RespCode": "00", "RespDesc": "Success" }, { "TransactionReferenceId": "201812203602", "VesselName": "122", "VoyageNumber": "DCTEST32", "RespCode": "00", "RespDesc": "Success" }, { "TransactionReferenceId": "201812203603", "VesselName": "122", "VoyageNumber": "DCTEST33", "RespCode": "00", "RespDesc": "Success" }, { "TransactionReferenceId": "201812203604", "VesselName": "122", "VoyageNumber": "DCTEST34", "RespCode": "00", "RespDesc": "Success" }, { "TransactionReferenceId": "201812203605", "VesselName": "122", "VoyageNumber": "DCTEST35", "RespCode": "00", "RespDesc": "Success" } ] }}

Above is sample mocked response

Hi,

Looking at your JSON payload, it has only 1 object, called "VesselScheduleResponse".

So your XML response will also has only 1 element, which look like:

<VesselScheduleResponse>
   ....
</VesselScheduleResponse>

Now within that object, 1 of its details is an array called "VesselDetails".

Since JSON specifies this array with a name, in your XML, the array looks like :

<VesselDetails>
    ...details of an element of array...
</VesselDetails>
<VesselDetails>
    ...details of an element of array...
</VesselDetails>

The net effect is your XML message is a single element, in which, 1 of its details is an array. This is expected:

<VesselScheduleResponse>
    ....
    <VesselDetails>
        ...details of an element of array...
    </VesselDetails>
    <VesselDetails>
        ...details of an element of array...
    </VesselDetails>
</VesselScheduleResponse>

More details on what are details to set up for JSONtoXML policy, refer to [1].

Another community post [2] explains this policy in clearer details.

[1] https://docs.apigee.com/api-platform/reference/policies/json-xml-policy#array

[2] https://community.apigee.com/articles/1839/converting-between-xml-and-json-what-you-need-to-k.html

The above thing is not working as expected.