0

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.

Harmandeep Singh Kalsi
  • 3,315
  • 2
  • 14
  • 26

1 Answers1

1

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

Hury Shen
  • 14,948
  • 1
  • 9
  • 18
  • Hury Shen, thank you for replying . I will try this and let you know . Also , there is addition conversion to XML involved I see in your approach , is there any mechanism where we directly convert JSON to XML rather than doing JSON -> XML -> Custom XML – Harmandeep Singh Kalsi Jun 22 '21 at 07:07
  • @HarmandeepSinghKalsi As far as I know, maybe we can also convert json to custom xml directly by xslt. But actually I'm not good at xslt, so I just use it to convert xml to customm xml. – Hury Shen Jun 22 '21 at 07:09
  • OK, Hury . I was checking this answer of yours , but this did not work for me , not sure if I missed something https://stackoverflow.com/questions/62649898/azure-apim-liquid-template-transform-json-to-xml-without-an-integration-acco – Harmandeep Singh Kalsi Jun 22 '21 at 07:44
  • @HarmandeepSinghKalsi I'm afraid you can't do it by the solution in the post you provided which I answered. Because `{"a":1,"b":2}` in your data is a map, it can't be loop(as array) to convert to ``. – Hury Shen Jun 22 '21 at 08:04
  • @HarmandeepSinghKalsi Yes, in that case the data is in a map, but the result also is a map(user is a map) in a array(users). But in your case, you want to convet the map `{"a":1,"b":2}` to `` which we need to loop the key-value of the map. So it is difficult to do it, we can't loop the key-value of the map. – Hury Shen Jun 22 '21 at 08:24
  • 1
    set-body with template="liquid" may also be used if payload is not recursive. – Vitaliy Kurokhtin Jun 22 '21 at 17:22
  • 1
    Hury, Vijay for FYI , I went ahead with the template=liquid and looped over the body and constructed my desired Output XML. – Harmandeep Singh Kalsi Jun 23 '21 at 03:10