How to remove a child element from json reponse in azue api management

Kannan Selvaraj 25 Reputation points
2023-06-28T13:44:53.59+00:00

I would like to remove the element "requestedDate" from the below response in azure apim.

{
    "head": {
        "requestedBy": "",
        "requestedDate": {
            "date": 27,
            "hours": 11,
            "seconds": 7,
            "month": 5,
            "timezoneOffset": -600,
            "year": 123,
            "minutes": 35,
            "time": 1687829707221,
            "day": 2
        },
        "type": "ErrorDetails",
        "errored": true
    },
    
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,449 questions
0 comments No comments
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,441 Reputation points Moderator
    2023-06-28T18:05:58.4633333+00:00

    Kannan Selvaraj Thanks for posting your question in Microsoft Q&A. I assume you are looking to remove requestedDate from head property in JSON and you can try the following code snippet (similar discussion):

    Code snippet:

    <set-body>@{
                var jsonResponse = context.Response.Body.As<JObject>(preserveContent: true);
                var headObject = (JObject)jsonResponse["head"];
    
                headObject.Properties()
                    .Where(x => x.Name.Contains("requestedDate"))
                    .ToList()
                    .ForEach(x => headObject.Remove(x.Name));
                   
                return jsonResponse.ToString();
              }</set-body>
    

    Output:

    User's image

    If you are looking to remove requestedDate from all the elements, you would need to loop through all properties of JSON response instead of just head property. Checkout JSON.NET (API management uses Newtonsoft.Json; .NET Framework types allowed) and samples to navigate through complex path. I hope this helps and let me know if any questions.


    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 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.