Replies: 6 comments 2 replies
-
In fact, the initial use case of this method was fetching the variable using the provided key as it is, and it's very helpful if your variable key contains a dot: The other method will not work, because it will try to get the nested variable b from the variable a. I created #34411 to add a small comment about this need. |
Beta Was this translation helpful? Give feedback.
-
@hussein-awala I appreciate the PR with change of documentation, but I still see it as an inconsistency that |
Beta Was this translation helpful? Give feedback.
-
They are two different paragraphs:
Here we explain that you can walk nested structures using In the second paragraph, we explain the you have another option to fetch the variable by key.
So here In my PR I explain that the second one it helpful if your key contains a dot, because you cannot access it by the first method.
|
Beta Was this translation helpful? Give feedback.
-
In this cases Some native Jinja templates sample import jinja2
environment = jinja2.Environment()
var = {"a": {"b": {"c": 1 }}}
template = environment.from_string("Sample a.b.c = {{ awesome_var.a.b.c }}")
print(template.render(awesome_var=var))
# Sample a.b.c = 1
template = environment.from_string("Sample ['a']['b']['c'] = {{ awesome_var['a']['b']['c'] }}")
print(template.render(awesome_var=var))
# Sample ['a']['b']['c'] = 1
template = environment.from_string("Sample mixin = {{ awesome_var.get('a')['b'].c }}")
print(template.render(awesome_var=var))
# Sample mixin = 1
template = environment.from_string("Sample by key = {{ awesome_var.get('a.b.c', 'Foo') }}")
print(template.render(awesome_var=var))
# Sample by key = Foo
template = environment.from_string("Sample by pseudo json-path = {{ awesome_var.get('a.b.c') }}")
print(template.render(awesome_var=var))
# Sample by pseudo json-path = None AFAIK |
Beta Was this translation helpful? Give feedback.
-
@Taragolis thank you for the clarification. So the getter with default value works only on the top level and it looks like it is currently not possible to use default/fallback variable values for missing parts of JSON content in Airflow? I [can do with if the variable is missing completely by providing the dictionary as a fallback (as demonstrated in the unit tests): But if any |
Beta Was this translation helpful? Give feedback.
-
For the future reference. The way to get the "getter" functionality with a nested json variables seems to be to use the full JINJA conditional syntax, checking for each level with a fallback value. This works, but is not really straightforward. |
Beta Was this translation helpful? Give feedback.
-
Apache Airflow version
2.7.1
What happened
According to Airflow Documentation on Airflow Variables in Templates there are two ways of accessing the JSON variables in templates:
{{ var.json.my_dict_var.key1 }}
{{{{ var.json.get('my.dict.var', {'key1': 'val1'}) }}
However, only the first approach works for nested variables, as demonstrated in the example.
Or is it me not understanding this documentation? Alternatively it could get updated to make it clearer.
What you think should happen instead
No response
How to reproduce
The following test demonstrates the issue:
Operating System
Ubuntu 22.04.3 LTS
Versions of Apache Airflow Providers
No response
Deployment
Virtualenv installation
Deployment details
No response
Anything else
No response
Are you willing to submit PR?
Code of Conduct
Beta Was this translation helpful? Give feedback.
All reactions