Check if key exists in a Python dict in Jinja2 templates
Solution 1
Like Mihai and karelv have noted, this works:
{% if 'blabla' in item %}
...
{% endif %}
I get a 'dict object' has no attribute 'blabla'
if I use {% if item.blabla %}
and item
does not contain a blabla
key
Solution 2
You can test for key definition this way:
{% if settings.property is defined %}
#...
{% endif %}
Solution 3
This works fine doesn't work in cases involving dictionaries. In those cases, please see the answer by tshalif.
Otherwise, with SaltStack (for example), you will get this error:
Unable to manage file: Jinja variable 'dict object' has no attribute '[attributeName]'
if you use this approach:
{% if settings.myProperty %}
note:
Will also skip, if settings.myProperty
exists, but is evaluated as False
(e.g. settings.myProperty = 0
).
Amal Antony
Updated on July 29, 2022Comments
-
Amal Antony almost 2 years
I have a python dictionary:
settings = { "foo" : "baz", "hello" : "world" }
This variable
settings
is then available in the Jinja2 template.I want to check if a key
myProperty
exists in thesettings
dict within my template, and if so take some action:{% if settings.hasKey(myProperty) %} takeSomeAction(); {% endif %}
What is the equivalent of
hasKey
that I can use? -
Amal Antony over 9 yearsCan
myProperty
be a variable in the approach above? -
Mihai Zamfir over 9 yearsYes, a key in a dictionary. Is the equivalent of
hasKey
. And btw, hasKey is deprecated. Usein
instead -
karelv over 8 yearswhen setting.myProperty exists, but it is equal to zero (integer 0) or to the boolean value False, this if test not for 'hasKey'. This means you need:
{% if 'myProperty' in settings %}
-
ryantuck about 8 yearsthis doesn't work for me (at least working with ansible). i found i needed
{% if settings.myProperty is defined %}
. -
chesterbr over 7 yearsSame for me. In my particular case I'm using Jinja2 inside Ansible.
-
Florian Neumann over 6 yearsI don't get why this is accepted as answer - it throws an error if the dict is missing the key. Please correct the answer.
-
Florian Neumann over 6 years@MihaiZamfir There is no reason to interpret my commentary with excessive emotion. tshalif prooved the failure - and his answer seems to be the more robust one and even if "the" answer is right for a subset of use cases, it should in my opinion at least reflect that it might not fit for all use cases.
-
Edward Fung over 6 yearsLooks like this answer fails if
settings.myProperty
is an empty dictionary/list. -
Drew about 6 yearsUsing jinja2 inside Ansible. Nothing helps. Both
{% if 'blabla' in item %}
and{% if item.blabla %}
return the same'dict object' has no attribute 'blabla'
wtf??? -
Marc Tamsky over 5 yearsAnswer by @RyanTuck is the most correct and works regardless of the Boolean (True/False) status of
myProperty
::{% if settings.myProperty is defined %}
-
ma3oun over 5 yearsfails when the key is not existent
-
blkpingu over 4 years
argument of type 'StringField' is not iterable
. Does not work for me -
Mayank Porwal almost 4 yearsThis looks like the correct way of checking if a key is present in dict or not. +1
-
Samha' about 3 yearsYour answer checks for object attributes, not dict keys.