# to\_json filter (sometimes) turns values into strings

**URL:** <https://forum.ansible.com/t/to-json-filter-sometimes-turns-values-into-strings/36239>\
**Category:** Ansible Project\
**Created:** [January 21, 2022, 8:51pm UTC](https://forum.ansible.com/t/to-json-filter-sometimes-turns-values-into-strings/36239 "2022-01-21T20:51:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jriendeau](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@jriendeau](https://forum.ansible.com/u/jriendeau)\
**Post date:** [January 21, 2022, 8:51pm UTC](https://forum.ansible.com/t/to-json-filter-sometimes-turns-values-into-strings/36239/1 "2022-01-21T20:51:00Z")

</div>

Hi all,

It seems like the to\_json filter behaves differently depending on:

- Whether it’s passed the result of a template call directly, or whether the result of the template call is placed into a fact first
- Whether the template call actually does any substitutions or not
- Whether I’m using ansible community \<=v4.10.0 or \>=5.0.1

I’m including a test playbook that illustrates the behavior below, along with the relevant portions of the output. I’m relatively new to Ansible, so wanted to check in here before submitting a bug report – this may be just a nuance I’m not grasping.

When I run the playbook using Ansible 4.10.0, to\_json turns the value into a JSON string, if and only if it’s passed the result of a template call which does no substitutions. If the template has a variable in it, or if the result of the template call is stored in a fact before being sent to to\_json, the JSON looks how I’d expect.

When using Ansible 5.0.1, the value is turned into a string whenever to\_json is passed the result of a template call, regardless of whether the template has a variable in it or not.

There’s a lot of permutations here, and I’m not sure which, if any, of these behaviors are unexpected. Thanks in advance for any help!

- John

**Test playbook:**

---

<div class="post-metadata">

**Author:** ![jriendeau](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@jriendeau](https://forum.ansible.com/u/jriendeau)\
**Post date:** [January 22, 2022, 1:45am UTC](https://forum.ansible.com/t/to-json-filter-sometimes-turns-values-into-strings/36239/2 "2022-01-22T01:45:41Z")

</div>

Apologies - just noticed that my test playbook had an error in it (was trying to simplify and clarify the test case and forgot to change the template file name in a couple places). It doesn’t change the behavior I’m seeing, but this is the corrected version:
