# Using JSON in a query string

**URL:** <https://forum.ansible.com/t/using-json-in-a-query-string/43789>\
**Category:** Get Help\
**Created:** [July 8, 2025, 8:31pm UTC](https://forum.ansible.com/t/using-json-in-a-query-string/43789 "2025-07-08T20:31:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sabman3](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sabman3/32/8863_2.png) [@Sabman3](https://forum.ansible.com/u/Sabman3)\
**Post date:** [July 8, 2025, 8:31pm UTC](https://forum.ansible.com/t/using-json-in-a-query-string/43789/1 "2025-07-08T20:31:12Z")

</div>

I’m trying to use the URI module with a query string that uses JSON as the value. I am using single quotes for the URI module param and double quotes for the JSON. However, the string is being used in the module as double quoted and the double quotes from the module param are being escaped. So the endpoint does not see it as valid JSON. Has anyone else ran into this issue?

- ansible.builtin.uri:  
url: ‘{{ base }}/api/v1/assets?\_limit=100&\_offset=0&\_filters=[{“id”:“name”,“includeValues”:[“{{ inventory\_hostname }}”],“excludeValues”:}]&with\_count=true&order=asc&orderColumns=name&showInactive=false’  
method: GET

From -vvv  
“url”: “[https://example.com/api/v1/assets?\_limit=100&\_offset=0&\_filters=[{\“id\”:\“name\”,\“includeValues\”:[\“example\_hostname\”],\“excludeValues\”:[]](https://example.com/api/v1/assets?_limit=100&_offset=0&_filters=%5B%7B%5C%22id%5C%22:%5C%22name%5C%22,%5C%22includeValues%5C%22:%5B%5C%22example_hostname%5C%22%5D,%5C%22excludeValues%5C%22:%5B%5D)}]&with\_count=true&order=asc&orderColumns=name&showInactive=false”,

---

<div class="post-metadata">

**Author:** ![rilindo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rilindo/32/357_2.png) [@rilindo](https://forum.ansible.com/u/rilindo)\
**Post date:** [July 9, 2025, 12:44am UTC](https://forum.ansible.com/t/using-json-in-a-query-string/43789/2 "2025-07-09T00:44:51Z")

</div>

This may not work (I don’t recall an API that accepts JSON), but have you considered defining the JSON as a YAML variable and then converting it to JSON? Something like:

```auto
ansible.builtin.uri:
  url: '{{ base }}/api/v1/assets?_limit=100&_offset=0&_filters={{ json_query | from_json }}&with_count=true&order=asc&orderColumns=name&showInactive=false'
  method: GET
  vars:
    json_query:
      id: "name"
      includeValues: 
        - "{{ inventory_hostname }}"
      excludeValues: []

```

---

<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:** [July 9, 2025, 10:11am UTC](https://forum.ansible.com/t/using-json-in-a-query-string/43789/3 "2025-07-09T10:11:16Z")

</div>

Does the API accept `POST` as well as `GET`? A `POST` might be easier to template?

---

<div class="post-metadata">

**Author:** ![Sabman3](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sabman3/32/8863_2.png) [@Sabman3](https://forum.ansible.com/u/Sabman3)\
**Post date:** [July 9, 2025, 2:35pm UTC](https://forum.ansible.com/t/using-json-in-a-query-string/43789/4 "2025-07-09T14:35:10Z")

</div>

I figured out my issue. It appears I had an extra double quote in the JSON. My Network team couldn’t get a firewall rule to work, so I was working on a system without an IDE and my old eyes missed that. The escapes did work once I took out the extra double quote.

---

<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 8, 2025, 2:35pm UTC](https://forum.ansible.com/t/using-json-in-a-query-string/43789/5 "2025-08-08T14:35:56Z")

</div>

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