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

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

a copy of the json would be helpful.

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”
}
]
}

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

  {{ 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.