### Summary

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

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.

I've found https://github.com/ansible/ansible/issues/76443, which seems closely related, but:
* I'm already using `template` and not `copy` as recommended [here](https://github.com/ansible/ansible/issues/76443#issuecomment-984889291)
* If it's the case that `to_json and to_nice_json ALWAYS are supposed to return a string (of serialized JSON)` as mentioned [here](https://github.com/ansible/ansible/issues/76443#issuecomment-984879201), it seems that there are edge cases where it doesn't

### Issue Type

Bug Report

### Component Name

to_json

### Ansible Version

```console
$ ansible --version
ansible [core 2.12.1]
  config file = None
  configured module search path = ['/Users/riendeau/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/riendeau/venvs/ansible5x/lib/python3.9/site-packages/ansible
  ansible collection location = /Users/riendeau/.ansible/collections:/usr/share/ansible/collections
  executable location = /Users/riendeau/venvs/ansible5x/bin/ansible
  python version = 3.9.10 (v3.9.10:f2f3f53782, Jan 13 2022, 17:02:14) [Clang 6.0 (clang-600.0.57)]
  jinja version = 3.0.3
  libyaml = True
```


### Configuration

```console
$ ansible-config dump --only-changed
COLOR_DEBUG(env: ANSIBLE_COLOR_DEBUG) = bright gray
```


### OS / Environment

Mac OS Monterey 12.1

### Steps to Reproduce

<!--- Paste example playbooks or commands between quotes below -->
```yaml (paste below)
---
- name: Test with trivial template
  hosts: localhost
  tasks:
  - copy:
      dest: "trivial.json.j2"
      content: '{ "hello": "world" }'
  - set_fact:
      template_result: "{{ lookup('template', 'trivial.json.j2') }}"

  - set_fact:
      json_from_fact_trivial_template: "{{ template_result | to_json }}"
      json_from_template_trivial_template: "{{ lookup('template', 'trivial.json.j2') | to_json }}"

  - debug:
      var: json_from_fact_trivial_template
  - debug:
      var: json_from_template_trivial_template

- name: Test with template including variable
  hosts: localhost
  tasks:
  - copy:
      dest: "withvar.json.j2"
      content: '{% raw %}{ "{{ greeting }}": "world" }{% endraw %}'
  - set_fact:
      greeting: 'howdy'
  - set_fact:
      template_result: "{{ lookup('template', 'withvar.json.j2') }}"
  - set_fact:
      json_from_fact_template_withvar: "{{ template_result | to_json }}"
      json_from_template_withvar: "{{ lookup('template', 'withvar.json.j2') | to_json }}"
  - debug:
      var: json_from_fact_template_withvar
  - debug:
      var: json_from_template_withvar
```

### Expected Results

I expected:
* Consistent behavior between Ansible 4.10.0 and Ansible 5.0.1, or documentation of a behavior change
* Consistent behavior within Ansible 5.0.1

### Actual Results

```console
(Output with Ansible 4.10.0:)

TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => {
    "json_from_fact_trivial_template": {
        "hello": "world"
    }
}

TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => {
    "json_from_template_trivial_template": "\"{ \\\"hello\\\": \\\"world\\\" }\""
}

TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => {
    "json_from_fact_template_withvar": {
        "howdy": "world"
    }
}

TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => {
    "json_from_template_withvar": {
        "howdy": "world"
    }
}

------

(Output with Ansible 5.0.1:)

TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => {
    "json_from_fact_trivial_template": {
        "hello": "world"
    }
}

TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => {
    "json_from_template_trivial_template": "\"{ \\\"hello\\\": \\\"world\\\" }\""
}

TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => {
    "json_from_fact_template_withvar": {
        "howdy": "world"
    }
}

TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => {
    "json_from_template_withvar": "\"{\\\"howdy\\\": \\\"world\\\"}\""
}
```


### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct
type: str
"""
EXAMPLES = """
- name: show templating results
  ansible.builtin.debug:
    msg: "{{ lookup('ansible.builtin.template', './some_template.j2') }}"
- name: show templating results with different variable start and end string
  ansible.builtin.debug:
    msg: "{{ lookup('ansible.builtin.template', './some_template.j2', variable_start_string='[%', variable_end_string='%]') }}"
- name: show templating results with different comment start and end string
  ansible.builtin.debug:
    msg: "{{ lookup('ansible.builtin.template', './some_template.j2', comment_start_string='[#', comment_end_string='#]') }}"
"""
RETURN = """
_raw:
   description: file(s) content after templating
   type: list
   elements: raw
"""
from copy import deepcopy
import os
import ansible.constants as C
from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
from ansible.module_utils._text import to_bytes, to_text
from ansible.template import generate_ansible_template_vars, AnsibleEnvironment
from ansible.utils.display import Display
from ansible.utils.native_jinja import NativeJinjaText
display = Display()
class LookupModule(LookupBase):