Home > front end >  Remove outside double quotes from list of jinja strings
Remove outside double quotes from list of jinja strings

Time:06-08

I'm formatting & appending strings to a list but the ref() function (used in dbt) isn't a string so I need to remove the outside quotes while retaining the inner single quotes.

I've attempted to chain strip() and replace() but neither are solving the problem.

Current code:

{% for result in results_list %}
    {% set result = "ref('{}')".format(result) %}
    {{ ref_format_results.append(result) }}
{% endfor %}

{% do log(ref_format_results, info=true) %}

Current output:

["ref('model_a')","ref('model_b')","ref('model_c')"]

Desired output:

[ref('model_a'),ref('model_b'),ref('model_c')]

CodePudding user response:

You're creating a string, which needs to be delimited somehow, and will always show outer quotes when printed/logged.

If you're templating those values into another file or string, then you'll already have what you want.

{% for r in ref_format_results %}
select * from {{ "{{" ~ r ~ "}}" }};
{% endfor %}

will produce:

select * from {{ ref('model_a') }};
select * from {{ ref('model_b') }};
select * from {{ ref('model_c') }};

But more importantly, why set result to a string in the first place? Are you trying to delay the execution of the ref() macro? If not, this will work just fine, to give you a list of evaluated refs (which are Relations):

{% for result in results_list %}
    {% set result = ref(result) %}
    {{ ref_format_results.append(result) }}
{% endfor %}

{% do log(ref_format_results, info=true) %}

Outputs:

['"my_db"."my_schema"."model_a"', '"my_db"."my_schema"."model_b"', '"my_db"."my_schema"."model_c"']
  • Related