# to\_json not working on variable

**URL:** <https://forum.ansible.com/t/to-json-not-working-on-variable/17117>\
**Category:** Ansible Project\
**Created:** [December 20, 2014, 9:42am UTC](https://forum.ansible.com/t/to-json-not-working-on-variable/17117 "2014-12-20T09:42:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dane\_Lipscombe](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@Dane\_Lipscombe](https://forum.ansible.com/u/Dane_Lipscombe)\
**Post date:** [December 20, 2014, 9:42am UTC](https://forum.ansible.com/t/to-json-not-working-on-variable/17117/1 "2014-12-20T09:42:08Z")

</div>

When running ansible v1.8.2, in the below playbook, to\_json fails to handle a basic variable

`

- name: a play that runs entirely on the ansible host  
hosts: localhost  
vars:  
users:
- bob
- joe  
tasks:

# no problem

- debug: msg=“{{users|to\_yaml}}”

# ERROR: an unexpected type error occurred. Error was Undefined is not JSON serializable

- debug: msg=“{{users|to\_json}}”

`

---

<div class="post-metadata">

**Author:** ![Dane\_Lipscombe](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@Dane\_Lipscombe](https://forum.ansible.com/u/Dane_Lipscombe)\
**Post date:** [December 23, 2014, 5:09am UTC](https://forum.ansible.com/t/to-json-not-working-on-variable/17117/2 "2014-12-23T05:09:44Z")

</div>

Could somebody please verify weather or not its just my problem?

---

<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:** [December 23, 2014, 12:44pm UTC](https://forum.ansible.com/t/to-json-not-working-on-variable/17117/3 "2014-12-23T12:44:20Z")

</div>

I can confirm this, even threw in:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug: var=users  
which works fine but still error out on your last task

---

<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:** [December 23, 2014, 12:50pm UTC](https://forum.ansible.com/t/to-json-not-working-on-variable/17117/4 "2014-12-23T12:50:31Z")

</div>

very weird, the following works:

- debug: msg="{{users|default('la')|to\_json}}"
