I have a Variable
stored like this:
x | {"a": 1, "b": 2}
I want to retrieve this full JSON as a dict in my DAG script. The following works:
from airflow.models import Variable
op = CustomOperator(
templated_field = Variable.get('x', deserialize_json = True)
)
# called in CustomOperator:
templated_field.keys()
# dict_keys(['a', 'b'])
The documentation suggests the following should be equivalent:
The second call [using
Variable.get(deserialize_json=True)
] assumes json content and will be deserialized......or if you need to deserialize a json object from the variable [using a Jinja template]:
{{ var.json.<variable_name> }}
However, when I use this and then try to extract the keys, I get error:
op = CustomOperator(
templated_field = '{{ var.json.x }}'
)
# called in CustomOperator
templated_field.keys()
AttributeError: 'str' object has no attribute 'keys'
The error suggests actually the json was not deserialized:
# same error
"{'a': 1, 'b': 2}".keys()
The only examples of using the var.json
approach I've found online don't extract a dict from JSON, but rather use a JSON path to extract a scalar:
# https://www.applydatascience.com/airflow/airflow-variables/
{{ var.json.example_variables_config.var3 }}
# https://gist.github.com/kaxil/61f41dd87a69230d1a637dc3a1d2fa2c
{{ var.json.dag1_config.var1 }}
Hossein helpfully points out that this field should be templated; here's the line from CustomOperator
:
class CustomOperator(SuperCustomOperator):
template_fields = SuperCustomOperator.template_fields + ('template_field',)
Am I missing something? Is the Jinja var.json
approach only suitable for extracting scalars, or can it be used to extract JSON-as-dict as well?