0

I am trying to figure out how to properly use the XML to JSON conversion in the Azure APIM Policy Editor. I've seen dozens of examples where all that's identified is this:

 <xml-to-json kind="direct" apply="always" consider-accept-header="false" />

However, I cannot find the appropriate place within my policy to place the code to make it do what I'm expecting. Here is my Outbound policy, which is where I am looking to use this:

<outbound>
    <base />
    <choose>
        <when condition="@(context.Response.StatusCode < 400)">
            <set-body template="liquid">
            {% if body.envelope.body.GetProjectsByQueryResponse.GetProjectsByQueryResult %}"{{body.envelope.body.GetProjectsByQueryResponse.GetProjectsByQueryResult | Replace: '\r', '\r' | Replace: '\n', '\n' | Replace: '([^\\](\\\\)*)"', '$1\"'}}"{% else %} null {% endif %}
            </set-body>
        </when>
        <otherwise>
            <set-variable name="old-body" value="@(context.Response.Body.As<string>(preserveContent: true))" />
            <!-- Error response as per https://github.com/Microsoft/api-guidelines/blob/master/Guidelines.md#7102-error-condition-responses -->
            <set-body template="liquid">{
        "error": {
            "code": "{{body.envelope.body.fault.faultcode}}",
            "message": "{{body.envelope.body.fault.faultstring}}"
        }
    }
    </set-body>
            <choose>
                <when condition="@(string.IsNullOrEmpty(context.Response.Body.As<JObject>(preserveContent: true)["error"]["code"].ToString()) && string.IsNullOrEmpty(context.Response.Body.As<JObject>(preserveContent: true)["error"]["message"].ToString()))">
                    <set-body>@{
                var newResponseBody = new JObject();
                newResponseBody["error"] = new JObject();
                newResponseBody["error"]["code"] = "InvalidErrorResponseBody";
                if (string.IsNullOrEmpty((string)context.Variables["old-body"]))
                {
                    newResponseBody["error"]["message"] = "The error response body was not a valid SOAP error response. The response body was empty.";
                }
                else
                {
                    newResponseBody["error"]["message"] = "The error response body was not a valid SOAP error response. The response body was: '" + context.Variables["old-body"] + "'.";
                }
                return newResponseBody.ToString();
            }</set-body>
                </when>
            </choose>
        </otherwise>
    </choose>
    <set-header name="Content-Type" exists-action="override">
        <value>application/json</value>
    </set-header>
    <xml-to-json kind="direct" apply="always" consider-accept-header="false" />
</outbound>

As you can see I've tried placing the conversion line at the very end of the outbound policy, but it's not doing what I expect; I still have an xml document returned.

Any help here in understanding how to revise my policy to work would be wonderful.

jdarling20
  • 13
  • 1
  • 4
  • You're doing a few other transformations before xml-to-json has a chance to transform response. I'd suggest removing everything but xml-to-json and seeing how just this policy works for you and then start adding stuff back. – Vitaliy Kurokhtin Sep 28 '21 at 16:19

0 Answers0