# Looking for advice: json returned from API with and without brackets

**URL:** <https://forum.ansible.com/t/looking-for-advice-json-returned-from-api-with-and-without-brackets/28343>\
**Category:** Ansible Project\
**Created:** [August 16, 2018, 6:57pm UTC](https://forum.ansible.com/t/looking-for-advice-json-returned-from-api-with-and-without-brackets/28343 "2018-08-16T18:57:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![J-F\_Gaudreault](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@J-F\_Gaudreault](https://forum.ansible.com/u/J-F_Gaudreault)\
**Post date:** [August 16, 2018, 6:57pm UTC](https://forum.ansible.com/t/looking-for-advice-json-returned-from-api-with-and-without-brackets/28343/1 "2018-08-16T18:57:39Z")

</div>

Hi! I getting a json from an API request. I’ve registered the json in a var.

I’m trying to find a way to get the hostname in the json.  
I need to find it even if it has brackets or not.

I only got this far …

with brackets:

- name: test1  
debug:  
msg: “{{ nagios\_xi\_hosts | json\_query(‘json.host[0].host\_name’) }}”

no brackets:

- name: test2  
debug:  
var: nagios\_xi\_hosts.json.host.host\_name

---

<div class="post-metadata">

**Author:** ![Jonathan\_Lozada\_De\_L](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@Jonathan\_Lozada\_De\_L](https://forum.ansible.com/u/Jonathan_Lozada_De_L)\
**Post date:** [August 16, 2018, 7:01pm UTC](https://forum.ansible.com/t/looking-for-advice-json-returned-from-api-with-and-without-brackets/28343/2 "2018-08-16T19:01:40Z")

</div>

a copy of the json would be helpful.

---

<div class="post-metadata">

**Author:** ![J-F\_Gaudreault](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@J-F\_Gaudreault](https://forum.ansible.com/u/J-F_Gaudreault)\
**Post date:** [August 16, 2018, 7:12pm UTC](https://forum.ansible.com/t/looking-for-advice-json-returned-from-api-with-and-without-brackets/28343/3 "2018-08-16T19:12:49Z")

</div>

The json… returning a list of 2+ objects.  
When the API returns only one object, the brackets are gone.

{  
“nagios\_xi\_hosts.json.host”: [  
{  
“@attributes”: {  
“id”: “26128”  
},  
“action\_url”: {},  
“active\_checks\_enabled”: “1”,  
“address”: “1.1.1.1”,  
“alias”: “sldops0725”,  
“check\_interval”: “5”,  
“config\_type”: “1”,  
“display\_name”: “sldops0725”,  
“first\_notification\_delay”: “0”,  
“host\_name”: “sldops0725”,  
“icon\_image”: “redhat.png”,  
“icon\_image\_alt”: {},  
“instance\_id”: “1”,  
“is\_active”: “1”,  
“max\_check\_attempts”: “5”,  
“notes”: {},  
“notes\_url”: {},  
“notification\_interval”: “60”,  
“notifications\_enabled”: “1”,  
“passive\_checks\_enabled”: “1”,  
“retry\_interval”: “1”,  
“statusmap\_image”: “redhat.png”  
},  
{  
“@attributes”: {  
“id”: “14039”  
},  
“action\_url”: {},  
“active\_checks\_enabled”: “1”,  
“address”: “2.2.2.2”,  
“alias”: “sldops0196”,  
“check\_interval”: “5”,  
“config\_type”: “1”,  
“display\_name”: “sldops0196”,  
“first\_notification\_delay”: “0”,  
“host\_name”: “sldops0196”,  
“icon\_image”: “redhat.png”,  
“icon\_image\_alt”: {},  
“instance\_id”: “1”,  
“is\_active”: “1”,  
“max\_check\_attempts”: “5”,  
“notes”: {},  
“notes\_url”: {},  
“notification\_interval”: “60”,  
“notifications\_enabled”: “1”,  
“passive\_checks\_enabled”: “1”,  
“retry\_interval”: “1”,  
“statusmap\_image”: “redhat.png”  
}  
]  
}

---

<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:** [August 16, 2018, 7:41pm UTC](https://forum.ansible.com/t/looking-for-advice-json-returned-from-api-with-and-without-brackets/28343/4 "2018-08-16T19:41:17Z")

</div>

But when it return multiple entries, which one of them will you have?  
If it's the fist element in the list this should work

&nbsp;&nbsp;{{ nagios\_xi\_hosts.json.host.host\_name | default(nagios\_xi\_hosts.json.host.0.host\_name }}

If nagios\_xi\_hosts.json.host.host\_name doesn't exist it will return the host\_name in the first list element.
