When to use from_json filter in Ansible?
<p>When should I use the <code>from_json</code> <a href="https://docs.ansible.com/ansible/playbooks_filters.html#filters-for-formatting-data" rel="noreferrer">filter</a> in Ansible?</p>

<p>I found out that using it sometimes has and sometimes have no effect.</p>

<p>Please consider the following example which illustrates the inconsistency I am getting.</p>

<p><sup>Included in reverse order are: the questions - expected result - actual result - the playbook - the data. The data is taken from <a href="https://stackoverflow.com/q/41261680/2947502">this question</a> and the playbook is based on <a href="https://stackoverflow.com/a/41262712/2947502">this answer</a>.</sup></p>

<ul>
<li><p>The question(s):</p>

<p>Why storing the left part (before <code>json_query</code>) of the following expression in a variable and then using <code>json_query</code> on the variable causes the expression to be evaluated differently?</p>

<pre><code>"{{ lookup('file','test.json') | json_query(query) }}"
</code></pre>

<p>Why does adding <code>from_json</code> filter alter the results (but does not if processing a variable):</p>

<pre><code>"{{ lookup('file','test.json') | from_json | json_query(query) }}"
</code></pre></li>
<li><p>Expected result:</p>

<p>Last four tasks should give the same result. Alternatively, last two tasks should give the same result as previous two tasks.</p></li>
<li><p>Actual result (last four tasks only):</p>

<p>One task result differs.</p>

<pre><code>TASK [This query is run against lookup value with from_json stored in a variable] ***
ok: [localhost] =&gt; {
    "msg": [
        678
    ]
}

TASK [This query is run against lookup value without from_json stored in a variable] ***
ok: [localhost] =&gt; {
    "msg": [
        678
    ]
}

TASK [This query is run directly against lookup value with from_json] **********
ok: [localhost] =&gt; {
    "msg": [
        678
    ]
}

TASK [This query is run directly against lookup value without from_json - the result is empty - why?] ***
ok: [localhost] =&gt; {
    "msg": ""
}
</code></pre></li>
<li><p>The playbook:</p>

<pre><code>---
- hosts: localhost
  gather_facts: no
  connection: local
  tasks:   
    - set_fact:
        from_lookup_with_from_json: "{{ lookup('file','test.json') | from_json }}"

    - set_fact:
        from_lookup_without_from_json: "{{ lookup('file','test.json') }}"

    - name: Save the lookup value stored in a variable in a file for comparison
      copy: content="{{ from_lookup_with_from_json }}" dest=./from_lookup_with_from_json.txt

    - name: Save the lookup value stored in a variable in a file for comparison (they are the same)
      copy: content="{{ from_lookup_without_from_json }}" dest=./from_lookup_without_from_json.txt

    - name: This query is run against lookup value with from_json stored in a variable
      debug: msg="{{ from_lookup_with_from_json | json_query(query) }}"
      vars:
        query: "Foods[].{id: Id, for: (Tags[?Key=='For'].Value)[0]} | [?for=='Tigger'].id"

    - name: This query is run against lookup value without from_json stored in a variable
      debug: msg="{{ from_lookup_without_from_json | json_query(query) }}"
      vars:
        query: "Foods[].{id: Id, for: (Tags[?Key=='For'].Value)[0]} | [?for=='Tigger'].id"

    - name: This query is run directly against lookup value with from_json
      debug: msg="{{ lookup('file','test.json') | from_json | json_query(query) }}"
      vars:
        query: "Foods[].{id: Id, for: (Tags[?Key=='For'].Value)[0]} | [?for=='Tigger'].id"

    - name: This query is run directly against lookup value without from_json - the result is empty - why?
      debug: msg="{{ lookup('file','test.json') | json_query(query) }}"
      vars:
        query: "Foods[].{id: Id, for: (Tags[?Key=='For'].Value)[0]} | [?for=='Tigger'].id"
</code></pre></li>
<li><p>The data (<code>test.json</code>):</p>

<pre><code>{ "Foods" :
  [ { "Id": 456
    , "Tags":
      [ {"Key":"For", "Value":"Heffalump"}
      , {"Key":"Purpose", "Value":"Food"}
      ]
    }
  , { "Id": 678
    , "Tags":
      [ {"Key":"For", "Value":"Tigger"}
      , {"Key":"Purpose", "Value":"Food"}
      ]
    }
  , { "Id": 911
    , "Tags":
      [ {"Key":"For", "Value":"Roo"}
      , {"Key":"Purpose", "Value":"Food"}
      ]
    }
  ]
}
</code></pre></li>
</ul>