output json variable to file

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.

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”]}

Hi

It’s working fine. Thanks a lot.

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