# Ansible nested Dictionary parsing

**URL:** <https://forum.ansible.com/t/ansible-nested-dictionary-parsing/2594>\
**Category:** Get Help\
**Tags:** json, plugin, jinja2\
**Created:** [November 27, 2023, 4:36pm UTC](https://forum.ansible.com/t/ansible-nested-dictionary-parsing/2594 "2023-11-27T16:36:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vicks](https://avatars.discourse-cdn.com/v4/letter/v/838e76/32.png) [@vicks](https://forum.ansible.com/u/vicks)\
**Post date:** [November 27, 2023, 4:36pm UTC](https://forum.ansible.com/t/ansible-nested-dictionary-parsing/2594/1 "2023-11-27T16:36:56Z")

</div>

How to Parse deeply nested dictionary in Ansible to get key / value pair that is all nested inside ?

# My dictionary output:

parser\_output": {  
“changed”: false,  
“failed”: false,  
“parsed”: {  
“isis”: {  
“2”: {  
“vrf”: {  
“default”: {  
“interfaces”: {  
“FH0/0/0/0”: {  
“neighbors”: {  
“B6”: {  
“holdtime”: “24”,  
“ietf\_nsf”: “Unable”,  
“snpa”: “_PtoP_”,  
“state”: “Up”,  
“type”: “L1”  
}  
}  
},  
“FH0/0/0/1”: {  
“neighbors”: {  
“BF0”: {  
“holdtime”: “28”,  
“ietf\_nsf”: “Capable”,  
“snpa”: “_PtoP_”,  
“state”: “Up”,  
“type”: “L1”  
}  
}  
},  
“FH0/0/0/2”: {  
“neighbors”: {  
“B8”: {  
“holdtime”: “28”,  
“ietf\_nsf”: “Capable”,  
“snpa”: “_PtoP_”,  
“state”: “Up”,  
“type”: “L1”  
}  
}  
},  
“FH0/1/0/1”: {  
“neighbors”: {  
“BF1”: {  
“holdtime”: “25”,  
“ietf\_nsf”: “Capable”,  
“snpa”: “_PtoP_”,  
“state”: “Up”,  
“type”: “L1”  
}  
}  
},  
“FH0/1/0/2”: {  
“neighbors”: {  
“B8”: {  
“holdtime”: “22”,  
“ietf\_nsf”: “Capable”,  
“snpa”: “_PtoP_”,  
“state”: “Up”,  
“type”: “L1”  
}  
}  
},  
“HundredGigE0/0/0/23/0”: {  
“neighbors”: {  
“BU0”: {  
“holdtime”: “22”,  
“ietf\_nsf”: “Capable”,  
“snpa”: “_PtoP_”,  
“state”: “Up”,  
“type”: “L1”  
}  
}  
},  
“HundredGigE0/1/0/23/0”: {  
“neighbors”: {  
“BU1”: {  
“holdtime”: “20”,  
“ietf\_nsf”: “Capable”,  
“snpa”: “_PtoP_”,  
“state”: “Up”,  
“type”: “L1”  
}  
}  
}  
},  
“total\_neighbor\_count”: 7  
}  
}  
}  
}  
},

# My Code:

- name: Print neighbors output  
debug:  
msg:  
- Key: “{{ items.key }}”  
- value: “{{ items.value }}”  
loop: “{{ parser\_output | subelements() }}”

Trying to use subelements how doesnt seem to work.  
Any help here ?

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [November 27, 2023, 5:14pm UTC](https://forum.ansible.com/t/ansible-nested-dictionary-parsing/2594/2 "2023-11-27T17:14:24Z")

</div>

It might be worth formatting your example better, or maybe attaching a file or using pastebin.

Is this JSON? Have you tried playing with [Selecting JSON data: JSON queries — Ansible Documentation](https://docs.ansible.com/ansible/latest/collections/community/general/docsite/filter_guide_selecting_json_data.html) ?

---

<div class="post-metadata">

**Author:** ![jbericat](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jbericat/32/627_2.png) [@jbericat](https://forum.ansible.com/u/jbericat)\
**Post date:** [November 28, 2023, 1:09am UTC](https://forum.ansible.com/t/ansible-nested-dictionary-parsing/2594/3 "2023-11-28T01:09:07Z")

</div>

Hello @vicks, welcome to the Community!

Just wanted to complete @jrglynn2 answer (which is totally ok btw). If for any case you cannot install the jmespath library in order to use the `json_query` filter (e.g. you’re using an Ansible EE on a production environment not managed by you), you can also use the `selectattr` Jinja2 filter, which comes pretty handy on such cases. Refer to the docs for further examples:

[https://docs.ansible.com/ansible/latest/playbook\_guide/complex\_data\_manipulation.html](https://docs.ansible.com/ansible/latest/playbook_guide/complex_data_manipulation.html)
