Hiya
I have a template file consisting of lots of static JSON, with small bits that need to be filled by variables. There is a list in the file, which I would like to fill with a variable number of elements:
{
... lots of JSON ...
"theList": [
{
... lots more JSON
"something": "variable"
... lots more JSON
}
]
... lots of JSON
}
If the list consists of a single element, this will work:
{
... lots of JSON ...
"theList": [
%{ for this_function in function_names ~}
{
... lots more JSON
"something": ${this_function}
... lots more JSON
}
%{ endfor ~}
]
... lots of JSON
}
It does not work when there is more than a single element in the list however, since that would require a ,
after the first element. If I put a ,
in there at the end (just before the endfor
, then it will not work at all, since it will always end the list with a ,
, which is not valid JSON.
So I think I need a way to conditionally add a ,
to the JSON list, but only if it’s not the last element.
EDIT: Forgot to say that I am able to do this with the following code after the endfor
:
${ this_function == element(function_names, length(function_names)-1) ? "" : "," }
but this seems kind of mental.