Issue with forloop.Last in the set body of Api policy with template type liquid.

Anju Datey 0 Reputation points
2024-03-22T03:34:34.35+00:00

Within API policy in APIM, while working with set body with template type liquid, we are not able to find the Last or First element of array.

forloop.Last is giving empty result, if logic is not working properly in the below code. However Last filter is the supported in the policy as per the document https://learn.microsoft.com/en-us/azure/api-management/set-body-policy#supported-liquid-filters

<set-body template="liquid">
            {
            "Document": {
                "Details": [
            {% for data in body.Document.Data %}
                {% assign forLoopTest = forloop.Last %}
                {
                    "First": "{{forLoopTest}}",
                    "Second":"{{ "now" | Date: "%Y-%m-%d %H:%M" }}"
                }
                {% if forloop.Last == false %}
                    ,
                {% else %}
                {% endif %}
            {% endfor %}]
            }
            }
        </set-body>

Received Output:

{
  "Document": {
      "Details": [      
      {
          "First": "",
          "Second":"Y-23-22 3:3"
      }      
      {
          "First": "",
          "Second":"Y-23-22 3:3"
      }
    ]
  }
  }

Expected Result:

{
  "Document": {
      "Details": [      
      {
          "First": "",
          "Second":"Y-23-22 3:3"
      },   
      {
          "First": "",
          "Second":"Y-23-22 3:3"
      }
    ]
  }
  }

Though I am able to get the expected result for forloop.last through another way. In the other approach I uploaded the liquid file to integration account and using logic app for liquid transformation is working properly.

Below is the Liquid File code which I uploaded to integration account

{

"Document": {

    "Details": [

        {% for data in content.Document.Data %}

            {

                "First": "{{forloop.last}}",

                "Second":"{{ "now" | Date: "%Y-%m-%d %H:%M" }}"

            }

            {% if forloop.last == false %}

                ,

            {% else %}

            {% endif %}

        {% endfor %}]

}

}

Kindly help with this to find the last element in the array in the set body policy with template type liquid.

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

3 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Charlie Wei 3,335 Reputation points
    2024-03-22T14:27:56.3766667+00:00

    Hello Anju Datey,

    Please refer to the modified code below.

    <set-body template="liquid">
    {
        "Document": {
            "Details": [
                {% assign test = "1, 2, 3" | Split: ", " %}
                {% for num in test %}
                    {% assign forLoopTest = forloop.last %}
                    {
                        "First": "{{ forLoopTest }}",
                        "Second":"{{ "now" | Date: "%Y-%m-%d %H:%M" }}"
                    }{% if forloop.last != true %},{% endif %}
                {% endfor %}
            ]
        }
    }
    </set-body>
    

    Best regards,
    Charlie


    If you find my response helpful, please consider accepting this answer and voting yes to support the community. Thank you!

    0 comments No comments

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.