If you see an error such as Container customer can only be accessed with constant string keys or Container reports can only be accessed with constant string keys, your Jinja code uses a dynamic key where platform expects a fixed string key.
Understand what causes the error
In Jinja, the primary key used to access iterable data structures must be a constant. This also affects containers such as customer or reports and other Jinja objects when you try to use a dynamic key.
This error occurs when accessing a built-in container with a variable or dynamic value rather than a constant string.
For example, the following Jinja code works because the customer attribute key is a literal string written directly in the object reference:
{{ customer['first_name'] }}
{{ reports['698edd739bb2c34ae5b81e88'].rows }}This code fails because the key is from a variable:
{% set attr_name = 'first_name' %}
{{ customer[attr_name] }}
{% set report_id = params.report %}
{{ reports[report_id].rows }}Check whether your key is dynamic
Review the value inside the square brackets [].
If the value comes from any of these sources, the key is dynamic:
- A Jinja variable
- A parameter
- An event property
- A customer attribute
-
A value built with Jinja logic
If you use a dynamic key with a built-in container, the template returns the constant string key error.
Use a supported workaround
If you need to switch between several known values, use if and elif statements and keep each container key hardcoded.
{# Customer attribute example #}
{% if params.attribute == 'first_name' %}
{{ customer['first_name'] }}
{% elif params.attribute == 'last_name' %}
{{ customer['last_name'] }}
{% endif %}
{# Report example #}
{% if params.report == 'A' %}
{% set rows = reports['REPORT_ID'].rows %}
{% elif params.report == 'B' %}
{% set rows = reports['OTHER_REPORT_ID'].rows %}
{% endif %}Use the same pattern if you need to switch between several known customer attributes or other supported keys.
Related documentation
For more information about Jinja data structures, read the Jinja data structures documentation.