XML-to-Json policy - remove namespaces

Nagashree Balasundaram 731 Reputation points
2023-03-31T15:14:20.39+00:00

The XML-to-Json policy in APIM converts the payload to json with the namespace in the attributes. Is there a way to eliminate the namespaces from the json while converting? Below is the converted payload from APIM

@MuthuKumaranMurugaachari-MSFT

@JananiRamesh-MSFT

{
    "RESPONSE_GROUP": {
        "xmlns$xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "xmlns$xsd": "http://www.w3.org/2001/XMLSchema",
        "RequestID": "c016d892-a232-4a77-a260-f7f6e39d5d2b",
        "VersionID": "1.0.0",
        "xmlns": "http://www.xxxx.com/Schema/DPN",
        "STATUS": {
            "StatusCode": "-1",
            "StatusDescription": "Internal Server Error",
            "StatusComment": "Internal Error occured while processing the order id: [c016d892-a232-4a77-a260-f7f6e39d5d2b], please reach out to support team with error reference Id:[7841b8fe-e6d9-43a9-ab94-84bbeaa994fa] for more details.",
            "xmlns": ""
        },
        "RESPONSE_PRODUCT": {
            "xmlns": ""
        },
        "Payload": {
            "xmlns": "",
            "DOCUMENTS": null
        }
    }
}
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
1,792 questions
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,156 Reputation points
    2023-03-31T18:25:18.1766667+00:00

    Nagashree Balasundaram Currently, it is not possible to remove namespaces with XML-to-Json policy (no attributes exists xml-to-json-policy). But you can write custom policy expression to parse JObject within set-body and remove namespaces or other elements based on your need. Here is the sample policy snippet:

    <xml-to-json kind="direct" apply="always" consider-accept-header="false" />
            <set-body>@{
                    var request = context.Request.Body.As<JObject>();
                    var obj = (JObject)request.SelectToken("RESPONSE_GROUP");
                    obj.Property("@xmlns$xsi").Remove();
                    obj.Property("@xmlns$xsd").Remove();
                    obj.Property("@xmlns").Remove();
                    return request.ToString();
                }
            </set-body>
    

    Note, this is just for reference, and you may have to loop through the elements and validate before attempting to remove them. Refer JSON.NET documentation for more info on how to parse/remove. Feel free to submit your feedback or idea directly to our product team via https://aka.ms/apimwish. I hope this helps with your questions and let me know if you have any.


    If you found the answer to your question helpful, please take a moment to mark it as "Yes" for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful