Templating JSON to send with uri

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

This is the code I have:

task.yml:

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.

actually you want |to_json filter.

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

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.

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

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}’