# Null Check before loop

**URL:** <https://forum.ansible.com/t/null-check-before-loop/28080>\
**Category:** Ansible Project\
**Created:** [July 18, 2018, 11:26am UTC](https://forum.ansible.com/t/null-check-before-loop/28080 "2018-07-18T11:26:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tommy.lin](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@tommy.lin](https://forum.ansible.com/u/tommy.lin)\
**Post date:** [July 18, 2018, 11:26am UTC](https://forum.ansible.com/t/null-check-before-loop/28080/1 "2018-07-18T11:26:49Z")

</div>

Hi Everyone,

How can i do a null check before going into the loop to avoid “obj must be a list of dicts or a nested dict” error

Thanks

---

<div class="post-metadata">

**Author:** ![tommy.lin](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@tommy.lin](https://forum.ansible.com/u/tommy.lin)\
**Post date:** [July 18, 2018, 11:31am UTC](https://forum.ansible.com/t/null-check-before-loop/28080/2 "2018-07-18T11:31:24Z")

</div>

I have tried this but not work

- name: Download packages  
win\_get\_url:  
url: “{{ item.1 }}”  
dest: C:\temp\{{ item.0.type }}.zip  
loop: “{{ result.targets|subelements(‘url’) }}”  
loop\_control:  
index\_var: my\_idx  
when: “‘targets’ in result”

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [July 18, 2018, 12:30pm UTC](https://forum.ansible.com/t/null-check-before-loop/28080/3 "2018-07-18T12:30:47Z")

</div>

Hard to say without seeing what is in your result var but you probably need to make use of default filter

something like

“{{ result.targets|default()|subelements(‘url’) }}”

perhaps, but I’m guessing a bit as I don’t know which bit of the results structure is not present.

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![tommy.lin](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@tommy.lin](https://forum.ansible.com/u/tommy.lin)\
**Post date:** [July 19, 2018, 3:16am UTC](https://forum.ansible.com/t/null-check-before-loop/28080/4 "2018-07-19T03:16:41Z")

</div>

Hi Jon,

It works now. Thanks a lot.
