Harmandeep Singh Kalsi
Harmandeep Singh Kalsi

Reputation: 3345

Azure APIM : Convert JSON Response to Customized XML Format

I have a requirement where I want to convert the JSON response , which is an array of object , to the customized XML format , so that my already existing code can parse it.

I know there is a Azure Transformation Policy named <json-to-xml /> , but there is no customization possible with it.

Sample JSON Response:

{
    "data":[
               {"a":1,"b":2},
               {"a":3,"b":4}
            ],
    "param2": "Success",
    "param3": "true"
 }

Desired XML Format:

<result>
 <sub-res>
  <res x="a" y=1>
  <res x="b" y=2>
 </sub-res>
 <sub-res>
  <res x="a" y=3>
  <res x="b" y=4>
 </sub-res>
</result>

I have tried using the liquid template as well but no success. Need guidance or pointers on this.

Upvotes: 0

Views: 1660

Answers (1)

Hury Shen
Hury Shen

Reputation: 15754

For this requirement, I created an api which response {"data":[{"a":1,"b":2},{"a":3,"b":4}]} to simulate your situation.

Then I use a <json-to-xml> in APIM policy first, the response will be convert to xml shown as below after the <json-to-xml> policy:

<Document>
    <data>
        <a>1</a>
        <b>2</b>
    </data>
    <data>
        <a>3</a>
        <b>4</b>
    </data>
</Document>

After that, use xslt to convert the xml to which you want.

Below is all of policy in my APIM for your reference:

enter image description here

The result of APIM show as what you want:

enter image description here

Upvotes: 1

Related Questions