# Templating JSON to send with uri

**URL:** <https://forum.ansible.com/t/templating-json-to-send-with-uri/20505>\
**Category:** Ansible Project\
**Created:** [February 10, 2016, 7:10am UTC](https://forum.ansible.com/t/templating-json-to-send-with-uri/20505 "2016-02-10T07:10:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Resnick](https://avatars.discourse-cdn.com/v4/letter/d/838e76/32.png) [@David\_Resnick](https://forum.ansible.com/u/David_Resnick)\
**Post date:** [February 10, 2016, 7:10am UTC](https://forum.ansible.com/t/templating-json-to-send-with-uri/20505/1 "2016-02-10T07:10:58Z")

</div>

I’m trying to POST some JSON that I have in a file.

This is the code I have:

task.yml:

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [February 10, 2016, 2:41pm UTC](https://forum.ansible.com/t/templating-json-to-send-with-uri/20505/2 "2016-02-10T14:41:09Z")

</div>

When specifying body\_format: json, the uri module will JSON encode the body. Your body is already JSON encoded, so it is being double encoded.

Try using from\_json on your lookup such as:

“{{ lookup(‘template’, …)|from\_json }}”

Or remove body\_format from your uri task, which may require you to set the content type header explicitly.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 10, 2016, 3:35pm UTC](https://forum.ansible.com/t/templating-json-to-send-with-uri/20505/3 "2016-02-10T15:35:15Z")

</div>

actually you want |to\_json filter.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [February 10, 2016, 3:38pm UTC](https://forum.ansible.com/t/templating-json-to-send-with-uri/20505/4 "2016-02-10T15:38:48Z")

</div>

I think that would end up triple json encoding it. The uri module is basically expecting a dict when you supply body\_format=json.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 10, 2016, 3:52pm UTC](https://forum.ansible.com/t/templating-json-to-send-with-uri/20505/5 "2016-02-10T15:52:22Z")

</div>

The JSON from the template (w/o filters) will be ‘typed’ by Ansible into a Python data structure, but if it has to\_json it will be kept as string bypassing the typing. The from\_json will convert it to a Python data structure before Ansible attempts to type it.

---

<div class="post-metadata">

**Author:** ![David\_Resnick](https://avatars.discourse-cdn.com/v4/letter/d/838e76/32.png) [@David\_Resnick](https://forum.ansible.com/u/David_Resnick)\
**Post date:** [February 17, 2016, 4:16pm UTC](https://forum.ansible.com/t/templating-json-to-send-with-uri/20505/6 "2016-02-17T16:16:48Z")

</div>

“{{ lookup(‘template’,‘add-datasource.json’) | from\_json }}” worked.

Thanks for responding so quickly; I didn’t notice the replies because notifications weren’t enabled.

- David

---

<div class="post-metadata">

**Author:** ![Giri\_Chintala](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@Giri\_Chintala](https://forum.ansible.com/u/Giri_Chintala)\
**Post date:** [February 17, 2016, 10:07pm UTC](https://forum.ansible.com/t/templating-json-to-send-with-uri/20505/7 "2016-02-17T22:07:23Z")

</div>

try adding “\_hack”:null

example

body: ‘{“pattern”:“^{{item.0.queue}}$”, “definition”:{“ha-mode”:“all”, “ha-sync-mode”:“automatic”, “federation-upstream-set”: “all”}, “priority”: 100000, “apply-to”: “queues”, “\_hack”:null}’
