# Prevent ansible from re-ordering JSON?

**URL:** <https://forum.ansible.com/t/prevent-ansible-from-re-ordering-json/28956>\
**Category:** Ansible Project\
**Created:** [November 6, 2018, 1:20pm UTC](https://forum.ansible.com/t/prevent-ansible-from-re-ordering-json/28956 "2018-11-06T13:20:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eddie\_Mashayev](https://avatars.discourse-cdn.com/v4/letter/e/8dc957/32.png) [@Eddie\_Mashayev](https://forum.ansible.com/u/Eddie_Mashayev)\
**Post date:** [November 6, 2018, 1:20pm UTC](https://forum.ansible.com/t/prevent-ansible-from-re-ordering-json/28956/1 "2018-11-06T13:20:15Z")

</div>

Hey,

I’ve this weird situation when I’m passing json(as a string) it still reordering it’s order and not consistent.

Example:

```auto
my_var: |
  {
    "b": 1
    "a": 2
  }

```

when running ansible with  
`

- name: “Set up json file”  
copy:  
dest: “/tmp/file.json”  
content: “{{ my\_var }}”

`

every ansible run the file is being changed(because json order is being changed), in order to fix it I’m using:  
`content: "{{ my_var | from_json | to_nice_json }}"`

This command keeps the order the same each run, because [to\_nice\_json](https://github.com/ansible/ansible/blob/80d977bac65768bdd5cd675b9d1794640749431a/lib/ansible/plugins/filter/core.py#L85) sorts the output (`sort_keys=True`).

My question is why **my\_var** is being changed all the time even it’s a string and **not** json

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 6, 2018, 4:22pm UTC](https://forum.ansible.com/t/prevent-ansible-from-re-ordering-json/28956/2 "2018-11-06T16:22:01Z")

</div>

This is something Ansible is inherited from Python, dictionary in Python is unordered.

---

<div class="post-metadata">

**Author:** ![Eddie\_Mashayev](https://avatars.discourse-cdn.com/v4/letter/e/8dc957/32.png) [@Eddie\_Mashayev](https://forum.ansible.com/u/Eddie_Mashayev)\
**Post date:** [November 6, 2018, 6:16pm UTC](https://forum.ansible.com/t/prevent-ansible-from-re-ordering-json/28956/3 "2018-11-06T18:16:57Z")

</div>

Thanks.

---

<div class="post-metadata">

**Author:** ![Eddie\_Mashayev](https://avatars.discourse-cdn.com/v4/letter/e/8dc957/32.png) [@Eddie\_Mashayev](https://forum.ansible.com/u/Eddie_Mashayev)\
**Post date:** [November 7, 2018, 6:06am UTC](https://forum.ansible.com/t/prevent-ansible-from-re-ordering-json/28956/4 "2018-11-07T06:06:18Z")

</div>

Hey Kai,

The thing is that is not a **dictionary** but **string** , pay attention to the “|” after my\_var:

```auto
my_var: |
  {
    "b": 1
    "a": 2
  }

```

---

<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:** [November 7, 2018, 3:43pm UTC](https://forum.ansible.com/t/prevent-ansible-from-re-ordering-json/28956/5 "2018-11-07T15:43:43Z")

</div>

To 'keep it a string' content: "{{ my\_var|string }}" , from\_json  
actually forces conversion (which would be happening anyways)
