# Json\_query and hyphens issue

**URL:** <https://forum.ansible.com/t/json-query-and-hyphens-issue/43745>\
**Category:** Get Help\
**Tags:** templating\
**Created:** [July 5, 2025, 11:03pm UTC](https://forum.ansible.com/t/json-query-and-hyphens-issue/43745 "2025-07-05T23:03:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![RRohi](https://avatars.discourse-cdn.com/v4/letter/r/f19dbf/32.png) [@RRohi](https://forum.ansible.com/u/RRohi)\
**Post date:** [July 5, 2025, 11:03pm UTC](https://forum.ansible.com/t/json-query-and-hyphens-issue/43745/1 "2025-07-05T23:03:12Z")

</div>

Hello

I’ve trying to get it working for hours now and no luck.

I’m querying Proxmox LXC network info from the API and the structure is:

```json
json
  data
    ip-addresses
      lo: ip-address
      eth0: ip-address

```

The task looks like this:

```yml
- name: Get LXC IP address
  ansible.builtin.debug:
    var: item
  loop: '{{ lxclist.json | community.general.json_query("data[?name==`eth0`].ip-addresses") }}'
  tags: apitest

```

_NOTE: If I remove the `.ip-addresses` part, it works, I get the whole eth0 network info, but I want to get the IP address._

This generates this kind of error: `JMESPathError in json_query filter plugin:\nBad jmespath expression: Unknown token '-'`.

There’s an issue [here](https://github.com/jmespath/jmespath.py/issues/109) where someone has the same issue and they’re saying that’s how it should work and you should use quotes to make it work, but it won’t.

Here are the combinations I have tried:

```yml
'{{ lxclist.json | community.general.json_query("data[?name==`eth0`].\"ip-addresses\"") }}'

```

Error: `template error while templating string: expected token ',', got 'ip'. String: {{ lxclist.json | community.general.json_query(\"data[?name==`eth0`].\\\\\"ip-addresses\\\\\"\"`

```yml
'{{ lxclist.json | community.general.json_query("data[?name==`eth0`].""ip-addresses""") }}'

```

Error: `JMESPathError in json_query filter plugin:\nBad jmespath expression: Unknown token '-'`

```yml
"{{ lxclist.json | community.general.json_query('data[?name==`eth0`].ip-addresses') }}"

```

Error: `JMESPathError in json_query filter plugin:\nBad jmespath expression: Unknown token '-'`

```yml
"{{ lxclist.json | community.general.json_query('data[?name==`eth0`].`ip-addresses`') }}"

```

Error: `JMESPathError in json_query filter plugin:\nExpecting: ['quoted_identifier', 'unquoted_identifier', 'lbracket', 'lbrace'], got: literal: Parse error at column 20, token \"ip-addresses\" (LITERAL)`

---

<div class="post-metadata">

**Author:** ![RRohi](https://avatars.discourse-cdn.com/v4/letter/r/f19dbf/32.png) [@RRohi](https://forum.ansible.com/u/RRohi)\
**Post date:** [July 6, 2025, 12:15am UTC](https://forum.ansible.com/t/json-query-and-hyphens-issue/43745/2 "2025-07-06T00:15:36Z")

</div>

omg… half a day of trial and error and this turned out to be the winning solution:  
To make it easier, I created a separate task that disables IPv6 on the remote host.

```yml
- name: Get LXC IP address.
  ansible.builtin.debug:
    msg: "{{ lxclist.json | community.general.json_query(query) }}"
  register: ipaddress
  tags: apitest
  vars:
    query: 'data[?name != `lo`]."ip-addresses"[*]."ip-address"'

```

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 6, 2025, 9:04am UTC](https://forum.ansible.com/t/json-query-and-hyphens-issue/43745/3 "2025-07-06T09:04:08Z")

</div>

This can be simplified

```yaml
- debug:
    var: result
  vars:
    _query: '*.eth0'
    result: "{{ lxclist.json.data | json_query(_query) }}"

```

Note: `query` is a reserved name.

---

<div class="post-metadata">

**Author:** ![RRohi](https://avatars.discourse-cdn.com/v4/letter/r/f19dbf/32.png) [@RRohi](https://forum.ansible.com/u/RRohi)\
**Post date:** [July 6, 2025, 11:11am UTC](https://forum.ansible.com/t/json-query-and-hyphens-issue/43745/4 "2025-07-06T11:11:06Z")

</div>

That doesn’t return anything, unfortunately. Doesn’t error, the value is null.

The whole JSON block from the previous task with a register named `lxclist`.

I’m trying to get the `ip-address` of the network interface where the name isn’t `lo`.

```json
"lxclist": {
	"cache_control": "max-age=0",
	"changed": false,
	"connection": "close",
	"content_length": "384",
	"content_type": "application/json;charset=UTF-8",
	"cookies": {},
	"cookies_string": "",
	"date": "Sun, 06 Jul 2025 10:49:24 GMT",
	"elapsed": 0,
	"expires": "Sun, 06 Jul 2025 10:49:24 GMT",
	"failed": false,
	"json": {
		"data": [
			{
				"hardware-address": "00:00:00:00:00:00",
				"hwaddr": "00:00:00:00:00:00",
				"inet": "127.0.0.1/8",
				"ip-addresses": [
					{
						"ip-address": "127.0.0.1",
						"ip-address-type": "inet",
						"prefix": "8"
					}
				],
				"name": "lo"
			},
			{
				"hardware-address": "00:24:00:07:a9:00",
				"hwaddr": "00:24:00:07:a9:00",
				"inet": "192.168.2.11/26",
				"ip-addresses": [
					{
						"ip-address": "192.168.2.11",
						"ip-address-type": "inet",
						"prefix": "26"
					}
				],
				"name": "eth0"
			}
		]
	},
	"msg": "OK (384 bytes)",
	"pragma": "no-cache",
	"redirected": false,
	"server": "pve-api-daemon/3.0",
	"status": 200,
	"url": "https://pmxhost.domain.local:8006/api2/json/nodes/pmxhost/lxc/106/interfaces"
}

```

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 6, 2025, 12:40pm UTC](https://forum.ansible.com/t/json-query-and-hyphens-issue/43745/5 "2025-07-06T12:40:33Z")

</div>

Then, you don’t need the filter `json_query`. For example,

```yaml
- # get the ip-address of the network interface where the name isn’t lo
  debug:
    var: result
  vars:
    result: "{{ lxclist.json.data | rejectattr('name', 'eq', 'lo')
                                  | map(attribute='ip-addresses')
                                  | flatten
                                  | map(attribute='ip-address') }}"

```

gives

```yaml
  result:
    - 192.168.2.11

```

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 6, 2025, 12:51pm UTC](https://forum.ansible.com/t/json-query-and-hyphens-issue/43745/6 "2025-07-06T12:51:01Z")

</div>

As a side note, you might want to create a dictionary first

```yaml
- debug:
    var: ip_addresses
  vars:
    _query: '[].[name, "ip-addresses"[]."ip-address"]'
    ip_addresses: "{{ dict(lxclist.json.data | json_query(_query)) }}"

```

gives

```yaml
  ip_addresses:
      eth0:
        - 192.168.2.11
      lo:
        - 127.0.0.1

```

Then, the searching is much easier.

---

<div class="post-metadata">

**Author:** ![RRohi](https://avatars.discourse-cdn.com/v4/letter/r/f19dbf/32.png) [@RRohi](https://forum.ansible.com/u/RRohi)\
**Post date:** [July 7, 2025, 4:24pm UTC](https://forum.ansible.com/t/json-query-and-hyphens-issue/43745/7 "2025-07-07T16:24:16Z")

</div>

```yml
- # get the ip-address of the network interface where the name isn’t lo
  debug:
    var: result
  vars:
    result: "{{ lxclist.json.data | rejectattr('name', 'eq', 'lo')
                                  | map(attribute='ip-addresses')
                                  | flatten
                                  | map(attribute='ip-address') }}

```

Thanks, I like that one because there’s no json\_query filter.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 6, 2025, 4:24pm UTC](https://forum.ansible.com/t/json-query-and-hyphens-issue/43745/8 "2025-08-06T16:24:54Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
