PRO, twig: Concat JSON-List to String

Challenge:

The API gives JSON with a List of items like this:

{"LocationList":[
{"LocationName":"AAAA"},
{"LocationName":"BBBB"},
{"LocationName":"CCCC"}
]}

The JCI-Plugin loads this JSON and all items need to be concatenated to one string.

Solution:

Use this twig-code:

Received data:<br>
{{ _context | json_encode  }}
<hr>
{% set concat_string = "" %}
{% for key,value in LocationList %}
    {% set concat_string = concat_string ~ (value.LocationName) %}
    {% if not loop.last %}
        {% set concat_string = concat_string ~ ","  %}
    {% endif %}
{% endfor %}
Concatenated String: {{concat_string}}

In Action:

Received data:
{“LocationList”:[{“LocationName”:”AAAA”},{“LocationName”:”BBBB”},{“LocationName”:”CCCC”}]}
Concatenated String: AAAA,BBBB,CCCC