# extracting data from dict in a loop

**URL:** <https://forum.ansible.com/t/extracting-data-from-dict-in-a-loop/34589>\
**Category:** Ansible Project\
**Created:** [February 8, 2021, 10:12pm UTC](https://forum.ansible.com/t/extracting-data-from-dict-in-a-loop/34589 "2021-02-08T22:12:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spiro\_Mitsialis](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@Spiro\_Mitsialis](https://forum.ansible.com/u/Spiro_Mitsialis)\
**Post date:** [February 8, 2021, 10:12pm UTC](https://forum.ansible.com/t/extracting-data-from-dict-in-a-loop/34589/1 "2021-02-08T22:12:44Z")

</div>

Hi,  
I am trying to extract certain information from a complex dictionary generated by a parser.  
For each interface which in the loop is the key, I would like to get the system\_name which is several layers deeper. I want to wildcard item.value.port\_id.\* but this does not work. How can I accomplish this? I would want the following result:

GigabitEthernet1/1/1 has system name accesspoint-3-ap1  
GigabitEthernet2/2/2 has system name accesspoint-4-ap2

- name: Test data extraction  
hosts: local  
gather\_facts: no  
vars:  
lldp1: {  
interfaces: {  
“GigabitEthernet1/1/1”: {  
“if\_name”: “GigabitEthernet1/1/1”,  
“port\_id”: {  
“AAAA.BBBB.0000”: {  
“neighbors”: {  
“accesspoint-3-ap1”: {  
“port\_description”: “bond0”,  
“port\_id”: “AAAA.BBBB.0000”,  
“system\_name”: “accesspoint-3-ap1”  
}  
}  
}  
}  
},  
“GigabitEthernet2/2/2”: {  
“if\_name”: “GigabitEthernet2/2/2”,  
“port\_id”: {  
“BBBB.CCCC.1111”: {  
“neighbors”: {  
“accesspoint-4-ap2”: {  
“port\_description”: “bond0”,  
“port\_id”: “BBBB.CCCC.1111”,  
“system\_name”: “accesspoint-4-ap2”  
}  
}  
}  
}  
}  
}  
}  
tasks:
- name: get data  
debug:  
msg: “{{ item.key }} has system name {{ item.value.port\_id.\*.neighbors.system\_name }}”  
loop: “{{ lldp1.interfaces | dict2items }}”

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 9, 2021, 7:08am UTC](https://forum.ansible.com/t/extracting-data-from-dict-in-a-loop/34589/2 "2021-02-09T07:08:07Z")

</div>

I'm sure that json\_query could be improved but this should work:

&nbsp;&nbsp;- name: get data  
&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.key }} has system name {{ item |  
json\_query('value.port\_id.\*.neighbors|[0]|\*.system\_name|[0]') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ lldp1.interfaces | dict2items }}"
