Skip to content

How to remove trailing commas

A common problem when generating JSON in a loop is the trailing comma, which is invalid JSON.

To avoid the trailing comma in loops, use if not loop.last provided by Tera.

example.tera
---
whiskers:
version: "^X.Y.Z"
---
{
"themes": [
{%- for id, flavor in flavors %}
{
"id": "catppuccin-{{id}}",
}{% if not loop.last %},{% endif %}
{%- endfor %}
]
}