Tuesday, September 8, 2020

SAP API Management - XML to JSON conversion

 Scenario: Target system returns the response in XML format. But Client wants the response in JSON format.

Sample XML Payload returned from Target:

<root>
<city>San Jose</city>
<firstName>John</firstName>
<lastName>Doe</lastName>
<state>CA</state>
</root>

Steps:
1. Add an instance of XMLTOJSON policy in Response path.




2. Modify the policy as per requirement.

In this case, code is modified as below:

<!-- This policy converts an XML payload to JSON structure -->
<XMLToJSON async="false" continueOnError="false" enabled="true" xmlns='http://www.sap.com/apimgmt'>
<Options>
<!-- The below three elements have to be used in conjunction if there is a namespace in the xml that is to undergo conversion -->
<NamespaceSeparator/>
<DefaultNamespaceNodeName/>
<NamespaceBlockName/>
<NullValue>NULL</NullValue>
<RecognizeNull>true</RecognizeNull>
<RecognizeNumber>true</RecognizeNumber>
<RecognizeBoolean>true</RecognizeBoolean>
<TreatAsArray>
<Path unwrap="false">root</Path>
</TreatAsArray>
</Options>
<!-- The variable to which the converted JSON should be assigned to -->
<OutputVariable>response</OutputVariable>
<!-- The variable that we want to convert to JSON -->
<Source>response</Source>
</XMLToJSON>

3. Test the API Proxy







No comments:

Post a Comment