title: Parsing JSON output from Juniper switch using Ansible
body: <p>I have this JSON output from Juniper switch where I need to get the remote system name associated with  <code>lldp_local_parent_interface_name</code> having the value <code>ae0</code>.<br />
  So, I only know the value <code>ae0</code> and I need to get the remote system name <code>A_B_C_D</code> in order to start targeting this host to execute some other tasks.</p>
  <p>The data, inside the file <em>juniper-data.json</em> looks like this:</p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;output&quot;: {
          &quot;lldp_neighbors_information&quot;: [{
              &quot;lldp_neighbor_information&quot;: [{
                      &quot;lldp_local_parent_interface_name&quot;: [{
                          &quot;data&quot;: &quot;ae0&quot;
                      }],
                      &quot;lldp_local_port_id&quot;: [{
                          &quot;data&quot;: &quot;xe_0/2/0&quot;
                      }],
                      &quot;lldp_remote_chassis_id&quot;: [{
                          &quot;data&quot;: &quot;00:00:00:00:00:00:00&quot;
                      }],
                      &quot;lldp_remote_chassis_id_subtype&quot;: [{
                          &quot;data&quot;: &quot;Mac address&quot;
                      }],
                      &quot;lldp_remote_port_description&quot;: [{
                          &quot;data&quot;: &quot;xe_1/0/1&quot;
                      }],
                      &quot;lldp_remote_system_name&quot;: [{
                          &quot;data&quot;: &quot;A_B_C_D&quot;
                      }]
                  },
                  {
                      &quot;lldp_local_parent_interface_name&quot;: [{
                          &quot;data&quot;: &quot;_&quot;
                      }],
                      &quot;lldp_local_port_id&quot;: [{
                          &quot;data&quot;: &quot;ge_0/0/23&quot;
                      }],
                      &quot;lldp_remote_chassis_id&quot;: [{
                          &quot;data&quot;: &quot;xx:xx:xx:xx:xx:xx&quot;
                      }],
                      &quot;lldp_remote_chassis_id_subtype&quot;: [{
                          &quot;data&quot;: &quot;Mac address&quot;
                      }],
                      &quot;lldp_remote_port_description&quot;: [{
                          &quot;data&quot;: &quot;bond0&quot;
                      }]
                  }
              ]
          }]
      }
  
  }
  </code></pre>
  <p>Here are my tasks:</p>
  <pre class="lang-yaml prettyprint-override"><code>- name: load data to var
    set_fact: 
      remote_sys_name: &quot;{{ lookup('file', 'juniper-data.json') | from_json }}&quot; 
  - name: view loaded data
    debug:
      var: item
    loop: &quot;{{ remote_sys_name | community.general.json_query('output.lldp_neighbors_information[0].lldp_neighbor_information[*].[?lldp_local_parent_interface_name[0].data=='ae0'].lldp_remote_system_name[0].data') }}&quot;
  </code></pre>
  <p>Expected results</p>
  <pre class="lang-yaml prettyprint-override"><code>&quot;item&quot;: &quot;A_B_C_D&quot;
  </code></pre>
  <p>What I got is</p>
  <blockquote>
  <p>fatal: [localhost]: FAILED! =&gt; {&quot;msg&quot;: &quot;template error while templating string: expected token ',', got 'ae0'. String: {{ remote_sys_name | community.general.json_query('output.lldp_neighbors_information[0].lldp_neighbor_information[*].[?lldp_local_parent_interface_name[0].data=='ae0'].lldp_remote_system_name[0].data') }}&quot;}</p>
  </blockquote>