how to extract a set of values from a json array in azure apim policies

Anonymous
2023-02-09T04:49:30.1766667+00:00

this is my input

{"DATA":[{"Number":"124345"},{"Number":"jhfgh788934"},{"Number":"tedjb789"}],"STATUS":{"SUCCESS":true,"CODE":""}}

Require all the values of numbers

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
1,751 questions
{count} votes

Accepted answer
  1. MayankBargali-MSFT 68,476 Reputation points
    2023-02-09T10:10:37.71+00:00

    @T V, Harsithaa Thanks for reaching out. APIM does policy expressions syntax in C# 7 as documented here and the supported .NET framework types are listed here.

    As per your input I have used the policy below that returns the array {"124345", "jhfgh788934", "tedjb789"} as per your requirement and I have provided input as per your example while calling my APIM API.

      <set-variable name="myaaray" value="@{
                        JObject input = context.Request.Body.As<JObject>(preserveContent: true);
                        JArray categories = (JArray)input["DATA"];
                        return new JArray(categories.Select(c => c["Number"]).ToList());
                    }" />
    

    Policy Debug Output :

    User's image

    Note: The above is for reference and please modify as per your business need.

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.

    0 comments No comments

0 additional answers

Sort by: Most helpful