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.