# output json variable to file

**URL:** <https://forum.ansible.com/t/output-json-variable-to-file/15233>\
**Category:** Ansible Project\
**Created:** [May 14, 2014, 1:58pm UTC](https://forum.ansible.com/t/output-json-variable-to-file/15233 "2014-05-14T13:58:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Khuong\_Dinh\_Pham](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Khuong\_Dinh\_Pham](https://forum.ansible.com/u/Khuong_Dinh_Pham)\
**Post date:** [May 14, 2014, 1:58pm UTC](https://forum.ansible.com/t/output-json-variable-to-file/15233/1 "2014-05-14T13:58:43Z")

</div>

Hi

I have this:

- some\_action: …

register: jsonformat

- debug: var=jsonformat
- local\_action: shell echo {{ jsonformat }} \> output

The json in console is displayed fine from debug action but the output file is not a valid json. It has some escape and unicode values. How can i output a valid json document to a file from a variable?

thx in advance.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [May 14, 2014, 3:35pm UTC](https://forum.ansible.com/t/output-json-variable-to-file/15233/2 "2014-05-14T15:35:45Z")

</div>

Use “{{ jsonformat|to\_json }}” to force the format to json.

Sample test:

- hosts: localhost  
gather\_facts: no  
vars:  
foo: ‘{“a”: [1,2,“3”]}’  
tasks:
- shell: echo “{{ foo|to\_json }}” \> /tmp/output

After running:

$ cat /tmp/output:  
{“a”: [1,2,“3”]}

---

<div class="post-metadata">

**Author:** ![Khuong\_Dinh\_Pham](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Khuong\_Dinh\_Pham](https://forum.ansible.com/u/Khuong_Dinh_Pham)\
**Post date:** [May 14, 2014, 8:37pm UTC](https://forum.ansible.com/t/output-json-variable-to-file/15233/3 "2014-05-14T20:37:42Z")

</div>

Hi

It’s working fine. Thanks a lot.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [May 14, 2014, 8:38pm UTC](https://forum.ansible.com/t/output-json-variable-to-file/15233/4 "2014-05-14T20:38:36Z")

</div>

the issue was probably the lack of quotes as unquoted json and shell don’t mix well​, you can probably ignore the to\_json.
