# parsing a nested json file

**URL:** <https://forum.ansible.com/t/parsing-a-nested-json-file/21984>\
**Category:** Ansible Project\
**Created:** [July 11, 2016, 1:07pm UTC](https://forum.ansible.com/t/parsing-a-nested-json-file/21984 "2016-07-11T13:07:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alix\_Ak](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@Alix\_Ak](https://forum.ansible.com/u/Alix_Ak)\
**Post date:** [July 11, 2016, 1:07pm UTC](https://forum.ansible.com/t/parsing-a-nested-json-file/21984/1 "2016-07-11T13:07:31Z")

</div>

hallo,

i have a nested json file like :  
{  
“fw1”: {  
“ipv4”: {  
“rtr”: {  
“ip”: “1.2.3.4”,  
“net”: “1.2.3.4”,

}  
}  
},  
“fw2”: {  
“ipv4”: {  
“rtr”: {  
“ip”: “4.3.2.1”,  
“net”: “4.3.2.1”,

}  
}  
}  
}

How could i parse in this case only the “ip” value of “fw2” ?  
for a non nested json dictionary this phrase has worked for me

```auto
{{ (lookup('file', '/etc/foo.txt') | from_json).get('ip') }}.

```

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 11, 2016, 3:41pm UTC](https://forum.ansible.com/t/parsing-a-nested-json-file/21984/2 "2016-07-11T15:41:30Z")

</div>

{{ (lookup('file', '/etc/foo.txt') | from\_json)['fw2']['ipv4']['rtr']['ip'] }}

or

{{ (lookup('file', '/etc/foo.txt') | from\_json).fw2.ipv4.rtr.ip }}

---

<div class="post-metadata">

**Author:** ![Alix\_Ak](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@Alix\_Ak](https://forum.ansible.com/u/Alix_Ak)\
**Post date:** [July 11, 2016, 5:16pm UTC](https://forum.ansible.com/t/parsing-a-nested-json-file/21984/3 "2016-07-11T17:16:57Z")

</div>

Thanks alot dude. It did work ^^  
i have recently got it working using lookup(‘file’, ‘/etc/foo.txt’) | from\_json).get(‘fw2’).get(‘ipv4’).get(‘rtr’).get(‘ip’) # haha though it didn’t look quit pretty  
i noticed if i am using an attribute with a minus sign i.e “fw2-m” the dot notation wouldn’t work.  
Just curious how could one overcome this issue?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 11, 2016, 5:49pm UTC](https://forum.ansible.com/t/parsing-a-nested-json-file/21984/4 "2016-07-11T17:49:40Z")

</div>

Python identifiers is not allowed to have dash in them(because dash is a subtraction), so only way around it in Ansible, is to use the square brackets notation.

---

<div class="post-metadata">

**Author:** ![Alix\_Ak](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@Alix\_Ak](https://forum.ansible.com/u/Alix_Ak)\
**Post date:** [July 12, 2016, 11:51am UTC](https://forum.ansible.com/t/parsing-a-nested-json-file/21984/5 "2016-07-12T11:51:56Z")

</div>

can u please tell me why should one add a single parentheses to “from\_json\*\*)\*\*.foo” ?
