# Community.general.json\_query returns the text "Hello World!"

**URL:** <https://forum.ansible.com/t/community-general-json-query-returns-the-text-hello-world/11046>\
**Category:** Get Help\
**Tags:** community-general\
**Created:** [November 21, 2024, 7:33pm UTC](https://forum.ansible.com/t/community-general-json-query-returns-the-text-hello-world/11046 "2024-11-21T19:33:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![spam-n-eggs](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/spam-n-eggs/32/5526_2.png) [@spam-n-eggs](https://forum.ansible.com/u/spam-n-eggs)\
**Post date:** [November 21, 2024, 7:33pm UTC](https://forum.ansible.com/t/community-general-json-query-returns-the-text-hello-world/11046/1 "2024-11-21T19:33:39Z")

</div>

When running a query on a json data structure, the return value is “Hello World!”  
Query:

```yaml
- name: Debug the Ethernet Full Results
  ansible.builtin.debug:
    var: "{{ ethernet_details | json_query(\"results[*].stdout_lines.ipv4.method\") }}"

```

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [November 21, 2024, 8:36pm UTC](https://forum.ansible.com/t/community-general-json-query-returns-the-text-hello-world/11046/2 "2024-11-21T20:36:34Z")

</div>

You are misusing `var`; it is intended to be the name of a variable, and can be abused to be a Jinja expression, but because you have wrapped the expression in `{{ }}` you are passing neither. You should instead be using `msg`:

```yaml
- name: Debug the Ethernet Full Results
  ansible.builtin.debug:
    msg: "{{ ethernet_details | json_query(\"results[*].stdout_lines.ipv4.method\") }}"

```

The next part of your problem is that your `json_query` is returning an empty string. You didn’t provide any sample data so it’s impossible to say definitively what you’re doing wrong, but as a general piece of advice: `json_query()` is rarely necessary and does not directly help you deal with JSON data.

Taking a wild guess at the actual data structure you’re working with, you might have meant something more like this:

```yaml
- name: Debug the Ethernet Full Results
  ansible.builtin.debug:
    msg: "{{ ethernet_details.results | map(attribute='stdout') | map('from_json') | map(attribute='ipv4.method') }}"
```

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [November 22, 2024, 9:48am UTC](https://forum.ansible.com/t/community-general-json-query-returns-the-text-hello-world/11046/3 "2024-11-22T09:48:31Z")

</div>

I find that using [JMESPath](https://jmespath.org/) is often easier and quicker than chaining Jinja2 filters together because you can use `jp` and even better [jpterm](https://github.com/jmespath-community/jmespath.terminal) for working out the JMESPath query without using Ansible (eg `jpterm foo.json`)…

Perhaps this might work?

```yaml
- name: Debug the Ethernet Full Results
  ansible.builtin.debug:
    var: ethernet_details | community.general.json_query('results[*].stdout_lines.ipv4.method')

```

However without knowing the content of `ethernet_details` I don’t know for sure…

---

<div class="post-metadata">

**Author:** ![spam-n-eggs](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/spam-n-eggs/32/5526_2.png) [@spam-n-eggs](https://forum.ansible.com/u/spam-n-eggs)\
**Post date:** [November 26, 2024, 5:49pm UTC](https://forum.ansible.com/t/community-general-json-query-returns-the-text-hello-world/11046/4 "2024-11-26T17:49:39Z")

</div>

Turned out that JMESPath wasn’t parsing it correctly the input data was a mess. It was the output from nmcli and was pseudo-json but missing some syntax items. Basically the output was more like this:

```json
"stdout_lines": [
                "connection.id: Wired connection 4",
                "connection.uuid: 3b6cdbf8-5cc2-3a94-a4f6-476359e2444d",
                "connection.stable-id: --",
                "connection.type: 802-3-ethernet",
                "connection.interface-name: enp0s9",
                "connection.autoconnect: yes",
                "connection.autoconnect-priority: -999",
                "connection.autoconnect-retries: -1 (default)",
                "connection.multi-connect: 0 (default)",
                "connection.auth-retries: -1",
                "connection.timestamp: 1732214614",
                "connection.read-only: no",
                "connection.permissions: --",
                "connection.zone: --",
                "connection.master: --",
                "connection.slave-type: --",
                "connection.autoconnect-slaves: -1 (default)",
                "connection.secondaries: --",
                "connection.gateway-ping-timeout: 0",
                "connection.metered: unknown",
                "connection.lldp: default",
                "connection.mdns: -1 (default)",
                "connection.llmnr: -1 (default)",
                "connection.dns-over-tls: -1 (default)",
                "connection.wait-device-timeout: -1",
                "802-3-ethernet.port: --",
                "802-3-ethernet.speed: 0",
                "802-3-ethernet.duplex: --",
                "802-3-ethernet.auto-negotiate: no",
                "802-3-ethernet.mac-address: --",
                "802-3-ethernet.cloned-mac-address: --",
                "802-3-ethernet.generate-mac-address-mask:--",
                "802-3-ethernet.mac-address-blacklist: --",
                "802-3-ethernet.mtu: auto",
                "802-3-ethernet.s390-subchannels: --",
                "802-3-ethernet.s390-nettype: --",
                "802-3-ethernet.s390-options: --",
                "802-3-ethernet.wake-on-lan: default",
                "802-3-ethernet.wake-on-lan-password: --",
                "802-3-ethernet.accept-all-mac-addresses:-1 (default)",
                "ipv4.method: auto",
                "ipv4.dns: --",
                "ipv4.dns-search: --",
                "ipv4.dns-options: --",
                "ipv4.dns-priority: 0",
                "ipv4.addresses: --",
                "ipv4.gateway: --",
                "ipv4.routes: --",
                "ipv4.route-metric: -1",
                "ipv4.route-table: 0 (unspec)",
                "ipv4.routing-rules: --",
                "ipv4.ignore-auto-routes: no",
                "ipv4.ignore-auto-dns: no",
                "ipv4.dhcp-client-id: --",
                "ipv4.dhcp-iaid: --",
                "ipv4.dhcp-timeout: 0 (default)",
                "ipv4.dhcp-send-hostname: yes",
                "ipv4.dhcp-hostname: --",
                "ipv4.dhcp-fqdn: --",
                "ipv4.dhcp-hostname-flags: 0x0 (none)",
                "ipv4.never-default: no",
                "ipv4.may-fail: yes",
                "ipv4.required-timeout: -1 (default)",
                "ipv4.dad-timeout: -1 (default)",
                "ipv4.dhcp-vendor-class-identifier: --",
                "ipv4.dhcp-reject-servers: --",
                "ipv6.method: auto",
                "ipv6.dns: --",
                "ipv6.dns-search: --",
                "ipv6.dns-options: --",
                "ipv6.dns-priority: 0",
                "ipv6.addresses: --",
                "ipv6.gateway: --",
                "ipv6.routes: --",
                "ipv6.route-metric: -1",
                "ipv6.route-table: 0 (unspec)",
                "ipv6.routing-rules: --",
                "ipv6.ignore-auto-routes: no",
                "ipv6.ignore-auto-dns: no",
                "ipv6.never-default: no",
                "ipv6.may-fail: yes",
                "ipv6.required-timeout: -1 (default)",
                "ipv6.ip6-privacy: -1 (unknown)",
                "ipv6.addr-gen-mode: stable-privacy",
                "ipv6.ra-timeout: 0 (default)",
                "ipv6.dhcp-duid: --",
                "ipv6.dhcp-iaid: --",
                "ipv6.dhcp-timeout: 0 (default)",
                "ipv6.dhcp-send-hostname: yes",
                "ipv6.dhcp-hostname: --",
                "ipv6.dhcp-hostname-flags: 0x0 (none)",
                "ipv6.token: --",
                "proxy.method: none",
                "proxy.browser-only: no",
                "proxy.pac-url: --",
                "proxy.pac-script: --"
            ]

```

I’m a little disappointed, but what can you do. I ended up having chain it in the end anyhow. I wish it would have allowed me to post it as an attachment in the end.

This was what I ended up with: `when: item.stdout_lines is defined and item.stdout | regex_findall("ipv4.method:\\s+auto") | length > 0`

---

<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:** [December 26, 2024, 5:50pm UTC](https://forum.ansible.com/t/community-general-json-query-returns-the-text-hello-world/11046/5 "2024-12-26T17:50:19Z")

</div>

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